text:
"""An implementation of the server side of the Chromium sync protocol.

The details of the protocol are described mostly by comments in the protocol
buffer definition at chrome/browser/sync/protocol/sync.proto.
"""

import cgi
import copy
import operator
import pickle
import random
import sys
import threading
import urlparse

import app_specifics_pb2
import autofill_specifics_pb2
import bookmark_specifics_pb2
import extension_specifics_pb2
import nigori_specifics_pb2
import password_specifics_pb2
import preference_specifics_pb2
import session_specifics_pb2
import sync_pb2
import theme_specifics_pb2
import typed_url_specifics_pb2

# An enumeration of the various kinds of data that can be synced.
# Over the wire, this enumeration is not used: a sync object's type is
# inferred by which EntitySpecifics extension it has.  But in the context
# of a program, it is useful to have an enumeration.
ALL_TYPES = (
    TOP_LEVEL,  # The type of the 'Google Chrome' folder.
    APPS,
    AUTOFILL,
    AUTOFILL_PROFILE,
    BOOKMARK,
    EXTENSIONS,
    NIGORI,
    PASSWORD,
    PREFERENCE,
    SESSION,
    THEME,
    TYPED_URL) = range(12)

# Well-known server tag of the top level 'Google Chrome' folder.
TOP_LEVEL_FOLDER_TAG = 'google_chrome'

# Given a sync type from ALL_TYPES, find the extension token corresponding
# to that datatype.  Note that TOP_LEVEL has no such token.
SYNC_TYPE_TO_EXTENSION = {
    APPS: app_specifics_pb2.app,
    AUTOFILL: autofill_specifics_pb2.autofill,
    AUTOFILL_PROFILE: autofill_specifics_pb2.autofill_profile,
    BOOKMARK: bookmark_specifics_pb2.bookmark,
    EXTENSIONS: extension_specifics_pb2.extension,
    NIGORI: nigori_specifics_pb2.nigori,
    PASSWORD: password_specifics_pb2.password,
    PREFERENCE: preference_specifics_pb2.preference,
    SESSION: session_specifics_pb2.session,
    THEME: theme_specifics_pb2.theme,
    TYPED_URL: typed_url_specifics_pb2.typed_url,
    }

# The parent ID used to indicate a top-level node.
ROOT_ID = '0'

class Error(Exception):
  """Error class for this module."""

class ProtobufExtensionNotUnique(Error):
  """An entry should not have more than one protobuf extension present."""

class DataTypeIdNotRecognized(Error):
  """The requested data type is not recognized."""

class MigrationDoneError(Error):
  """A server-side migration occurred; clients must re-sync some datatypes.

  Attributes:
    datatypes: a list of the datatypes (python enum) needing migration.
  """

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

class StoreBirthdayError(Error):
  """The client sent a birthday that doesn't correspond to this server."""

def GetEntryType(entry):
  """Extract the sync type from a SyncEntry.

  Args:
    entry: A SyncEntity protobuf object whose type to determine.
  Returns:
    An enum value from ALL_TYPES if the entry's type can be determined, or None
    if the type cannot be determined.
  Raises:
    ProtobufExtensionNotUnique: More than one type was indicated by the entry.
  """
  if entry.server_defined_unique_tag == TOP_LEVEL_FOLDER_TAG:
    return TOP_LEVEL
  entry_types = GetEntryTypesFromSpecifics(entry.specifics)
  if not entry_types:
    return None

  # If there is more than one, either there's a bug, or else the caller
  # should use GetEntryTypes.
  if len(entry_types) > 1:
    raise ProtobufExtensionNotUnique
  return entry_types[0]

def GetEntryTypesFromSpecifics(specifics):
  """Determine the sync types indicated by an EntitySpecifics's extension(s).

  If the specifics have more than one recognized extension (as commonly
  happens with the requested_types field of GetUpdatesMessage), all types
  will be returned.  Callers must handle the possibility of the returned
  value having more than one item.

  Args:
    specifics: A EntitySpecifics protobuf message whose extensions to
      enumerate.
  Returns:
    A list of the sync types (values from ALL_TYPES) associated with each
    recognized extension of the specifics message.
  """
  return [data_type for data_type, extension
          in SYNC_TYPE_TO_EXTENSION.iteritems()
          if specifics.HasExtension(extension)]

def SyncTypeToProtocolDataTypeId(data_type):
  """Convert from a sync type (python enum) to the protocol's data type id."""
  return SYNC_TYPE_TO_EXTENSION[data_type].number

def ProtocolDataTypeIdToSyncType(protocol_data_type_id):
  """Convert from the protocol's data type id to a sync type (python enum)."""
  for data_type, protocol_extension in SYNC_TYPE_TO_EXTENSION.iteritems():
    if protocol_extension.number == protocol_data_type_id:
      return data_type
  raise DataTypeIdNotRecognized

def DataTypeStringToSyncTypeLoose(data_type_string):
  """Converts a human-readable string to a sync type (python enum).

  Capitalization and pluralization don't matter; this function is appropriate
  for values that might have been typed by a human being; e.g., command-line
  flags or query parameters.
  """
  if data_type_string.isdigit():
    return ProtocolDataTypeIdToSyncType(int(data_type_string))
  name = data_type_string.lower().rstrip('s')
  for data_type, protocol_extension in SYNC_TYPE_TO_EXTENSION.iteritems():
    if protocol_extension.name.lower().rstrip('s') == name:
      return data_type
  raise DataTypeIdNotRecognized

def SyncTypeToString(data_type):
  """Formats a sync type enum (from ALL_TYPES) to a human-readable string."""
  return SYNC_TYPE_TO_EXTENSION[data_type].name

def CallerInfoToString(caller_info_source):
  """Formats a GetUpdatesSource enum value to a readable string."""
  return sync_pb2.GetUpdatesCallerInfo.DESCRIPTOR.enum_types_by_name[
      'GetUpdatesSource'].values_by_number[caller_info_source].name

def ShortDatatypeListSummary(data_types):
  """Formats compactly a list of sync types (python enums) for human eyes.

  This function is intended for use by logging.  If the list of datatypes
  contains almost all of the values, the return value will be expressed
  in terms of the datatypes that aren't set.
  """
  included = set(data_types) - set([TOP_LEVEL])
  if not included:
    return 'nothing'
  excluded = set(ALL_TYPES) - included - set([TOP_LEVEL])
  if not excluded:
    return 'everything'
  simple_text = '+'.join(sorted([SyncTypeToString(x) for x in included]))
  all_but_text = 'all except %s' % (
      '+'.join(sorted([SyncTypeToString(x) for x in excluded])))
  if len(included) < len(excluded) or len(simple_text) <= len(all_but_text):
    return simple_text
  else:
    return all_but_text

def GetDefaultEntitySpecifics(data_type):
  """Get an EntitySpecifics having a sync type's default extension value."""
  specifics = sync_pb2.EntitySpecifics()
  if data_type in SYNC_TYPE_TO_EXTENSION:
    extension_handle = SYNC_TYPE_TO_EXTENSION[data_type]
    specifics.Extensions[extension_handle].SetInParent()
  return specifics

class PermanentItem(object):
  """A specification of one server-created permanent item.

  Attributes:
    tag: A known-to-the-client value that uniquely identifies a server-created
      permanent item.
    name: The human-readable display name for this item.
    parent_tag: The tag of the permanent item's parent.  If ROOT_ID, indicates
      a top-level item.  Otherwise, this must be the tag value of some other
      server-created permanent item.
    sync_type: A value from ALL_TYPES, giving the datatype of this permanent
      item.  This controls which types of client GetUpdates requests will
      cause the permanent item to be created and returned.
  """

  def __init__(self, tag, name, parent_tag, sync_type):
    self.tag = tag
    self.name = name
    self.parent_tag = parent_tag
    self.sync_type = sync_type

class MigrationHistory(object):
  """A record of the migration events associated with an account.

  Each migration event invalidates one or more datatypes on all clients
  that had synced the datatype before the event.  Such clients will continue
  to receive MigrationDone errors until they throw away their progress and
  re-sync that datatype from the beginning.
  """
  def __init__(self):
    self._migrations = {}
    for datatype in ALL_TYPES:
      self._migrations[datatype] = [1]
    self._next_migration_version = 2

  def GetLatestVersion(self, datatype):
    return self._migrations[datatype][-1]

  def CheckAllCurrent(self, versions_map):
    """Raises an error if any the provided versions are out of date.

    This function intentionally returns migrations in the order that they were
    triggered.  Doing it this way allows the client to queue up two migrations
    in a row, so the second one is received while responding to the first.

    Arguments:
      version_map: a map whose keys are datatypes and whose values are versions.

    Raises:
      MigrationDoneError: if a mismatch is found.
    """
    problems = {}
    for datatype, client_migration in versions_map.iteritems():
      for server_migration in self._migrations[datatype]:
        if client_migration < server_migration:
          problems.setdefault(server_migration, []).append(datatype)
    if problems:
      raise MigrationDoneError(problems[min(problems.keys())])

  def Bump(self, datatypes):
    """Add a record of a migration, to cause errors on future requests."""
    for idx, datatype in enumerate(datatypes):
      self._migrations[datatype].append(self._next_migration_version)
    self._next_migration_version += 1

class UpdateSieve(object):
  """A filter to remove items the client has already seen."""
  def __init__(self, request, migration_history=None):
    self._original_request = request
    self._state = {}
    self._migration_history = migration_history or MigrationHistory()
    self._migration_versions_to_check = {}
    if request.from_progress_marker:
      for marker in request.from_progress_marker:
        data_type = ProtocolDataTypeIdToSyncType(marker.data_type_id)
        if marker.HasField('timestamp_token_for_migration'):
          timestamp = marker.timestamp_token_for_migration
          if timestamp:
            self._migration_versions_to_check[data_type] = 1
        elif marker.token:
          (timestamp, version) = pickle.loads(marker.token)
          self._migration_versions_to_check[data_type] = version
        elif marker.HasField('token'):
          timestamp = 0
        else:
          raise ValueError('No timestamp information in progress marker.')
        data_type = ProtocolDataTypeIdToSyncType(marker.data_type_id)
        self._state[data_type] = timestamp
    elif request.HasField('from_timestamp'):
      for data_type in GetEntryTypesFromSpecifics(request.requested_types):
        self._state[data_type] = request.from_timestamp
        self._migration_versions_to_check[data_type] = 1
    if self._state:
      self._state[TOP_LEVEL] = min(self._state.itervalues())

  def SummarizeRequest(self):
    timestamps = {}
    for data_type, timestamp in self._state.iteritems():
      if data_type == TOP_LEVEL:
        continue
      timestamps.setdefault(timestamp, []).append(data_type)
    return ', '.join('<%s>@%d' % (ShortDatatypeListSummary(types), stamp)
                     for stamp, types in sorted(timestamps.iteritems()))

  def CheckMigrationState(self):
    self._migration_history.CheckAllCurrent(self._migration_versions_to_check)

  def ClientWantsItem(self, item):
    """Return true if the client hasn't already seen an item."""
    return self._state.get(GetEntryType(item), sys.maxint) < item.version

  def HasAnyTimestamp(self):
    """Return true if at least one datatype was requested."""
    return bool(self._state)

  def GetMinTimestamp(self):
    """Return true the smallest timestamp requested across all datatypes."""
    return min(self._state.itervalues())

  def GetFirstTimeTypes(self):
    """Return a list of datatypes requesting updates from timestamp zero."""
    return [datatype for datatype, timestamp in self._state.iteritems()
            if timestamp == 0]

  def SaveProgress(self, new_timestamp, get_updates_response):
    """Write the new_timestamp or new_progress_marker fields to a response."""
    if self._original_request.from_progress_marker:
      for data_type, old_timestamp in self._state.iteritems():
        if data_type == TOP_LEVEL:
          continue
        new_marker = sync_pb2.DataTypeProgressMarker()
        new_marker.data_type_id = SyncTypeToProtocolDataTypeId(data_type)
        final_stamp = max(old_timestamp, new_timestamp)
        final_migration = self._migration_history.GetLatestVersion(data_type)
        new_marker.token = pickle.dumps((final_stamp, final_migration))
        if new_marker not in self._original_request.from_progress_marker:
          get_updates_response.new_progress_marker.add().MergeFrom(new_marker)
    elif self._original_request.HasField('from_timestamp'):
      if self._original_request.from_timestamp < new_timestamp:
        get_updates_response.new_timestamp = new_timestamp

class SyncDataModel(object):
  """Models the account state of one sync user."""
  _BATCH_SIZE = 100

  # Specify all the permanent items that a model might need.
  _PERMANENT_ITEM_SPECS = [
      PermanentItem('google_chrome', name='Google Chrome',
                    parent_tag=ROOT_ID, sync_type=TOP_LEVEL),
      PermanentItem('google_chrome_bookmarks', name='Bookmarks',
                    parent_tag='google_chrome', sync_type=BOOKMARK),
      PermanentItem('bookmark_bar', name='Bookmark Bar',
                    parent_tag='google_chrome_bookmarks', sync_type=BOOKMARK),
      PermanentItem('other_bookmarks', name='Other Bookmarks',
                    parent_tag='google_chrome_bookmarks', sync_type=BOOKMARK),
      PermanentItem('google_chrome_preferences', name='Preferences',
                    parent_tag='google_chrome', sync_type=PREFERENCE),
      PermanentItem('google_chrome_autofill', name='Autofill',
                    parent_tag='google_chrome', sync_type=AUTOFILL),
      PermanentItem('google_chrome_autofill_profiles', name='Autofill Profiles',
                    parent_tag='google_chrome', sync_type=AUTOFILL_PROFILE),
      PermanentItem('google_chrome_extensions', name='Extensions',
                    parent_tag='google_chrome', sync_type=EXTENSIONS),
      PermanentItem('google_chrome_passwords', name='Passwords',
                    parent_tag='google_chrome', sync_type=PASSWORD),
      PermanentItem('google_chrome_sessions', name='Sessions',
                    parent_tag='google_chrome', sync_type=SESSION),
      PermanentItem('google_chrome_themes', name='Themes',
                    parent_tag='google_chrome', sync_type=THEME),
      PermanentItem('google_chrome_typed_urls', name='Typed URLs',
                    parent_tag='google_chrome', sync_type=TYPED_URL),
      PermanentItem('google_chrome_nigori', name='Nigori',
                    parent_tag='google_chrome', sync_type=NIGORI),
      PermanentItem('google_chrome_apps', name='Apps',
                    parent_tag='google_chrome', sync_type=APPS),
      ]

  def __init__(self):
    # Monotonically increasing version number.  The next object change will
    # take on this value + 1.
    self._version = 0

    # The definitive copy of this client's items: a map from ID string to a
    # SyncEntity protocol buffer.
    self._entries = {}

    # TODO(nick): uuid.uuid1() is better, but python 2.5 only.
    self.store_birthday = '%0.30f' % random.random()

    self.migration_history = MigrationHistory()

  def _SaveEntry(self, entry):
    """Insert or update an entry in the change log, and give it a new version.

    The ID fields of this entry are assumed to be valid server IDs.  This
    entry will be updated with a new version number and sync_timestamp.

    Args:
      entry: The entry to be added or updated.
    """
    self._version += 1
    # Maintain a global (rather than per-item) sequence number and use it
    # both as the per-entry version as well as the update-progress timestamp.
    # This simulates the behavior of the original server implementation.
    entry.version = self._version
    entry.sync_timestamp = self._version

    # Preserve the originator info, which the client is not required to send
    # when updating.
    base_entry = self._entries.get(entry.id_string)
    if base_entry:
      entry.originator_cache_guid = base_entry.originator_cache_guid
      entry.originator_client_item_id = base_entry.originator_client_item_id

    self._entries[entry.id_string] = copy.deepcopy(entry)

  def _ServerTagToId(self, tag):
    """Determine the server ID from a server-unique tag.

    The resulting value is guaranteed not to collide with the other ID
    generation methods.

    Args:
      datatype: The sync type (python enum) of the identified object.
      tag: The unique, known-to-the-client tag of a server-generated item.
    Returns:
      The string value of the computed server ID.
    """
    if not tag or tag == ROOT_ID:
      return tag
    spec = [x for x in self._PERMANENT_ITEM_SPECS if x.tag == tag][0]
    return self._MakeCurrentId(spec.sync_type, '<server tag>%s' % tag)

  def _ClientTagToId(self, datatype, tag):
    """Determine the server ID from a client-unique tag.

    The resulting value is guaranteed not to collide with the other ID
    generation methods.

    Args:
      datatype: The sync type (python enum) of the identified object.
      tag: The unique, opaque-to-the-server tag of a client-tagged item.
    Returns:
      The string value of the computed server ID.
    """
    return self._MakeCurrentId(datatype, '<client tag>%s' % tag)

  def _ClientIdToId(self, datatype, client_guid, client_item_id):
    """Compute a unique server ID from a client-local ID tag.

    The resulting value is guaranteed not to collide with the other ID
    generation methods.

    Args:
      datatype: The sync type (python enum) of the identified object.
      client_guid: A globally unique ID that identifies the client which
        created this item.
      client_item_id: An ID that uniquely identifies this item on the client
        which created it.
    Returns:
      The string value of the computed server ID.
    """
    # Using the client ID info is not required here (we could instead generate
    # a random ID), but it's useful for debugging.
    return self._MakeCurrentId(datatype,
        '<server ID originally>%s/%s' % (client_guid, client_item_id))

  def _MakeCurrentId(self, datatype, inner_id):
    return '%d^%d^%s' % (datatype,
                         self.migration_history.GetLatestVersion(datatype),
                         inner_id)

  def _ExtractIdInfo(self, id_string):
    if not id_string or id_string == ROOT_ID:
      return None
    datatype_string, separator, remainder = id_string.partition('^')
    migration_version_string, separator, inner_id = remainder.partition('^')
    return (int(datatype_string), int(migration_version_string), inner_id)

  def _WritePosition(self, entry, parent_id, prev_id=None):
    """Convert from a relative position into an absolute, numeric position.

    Clients specify positions using the predecessor-based references; the
    server stores and reports item positions using sparse integer values.
    This method converts from the former to the latter.

    Args:
      entry: The entry for which to compute a position.  Its ID field are
        assumed to be server IDs.  This entry will have its parent_id_string
        and position_in_parent fields updated; its insert_after_item_id field
        will be cleared.
      parent_id: The ID of the entry intended as the new parent.
      prev_id: The ID of the entry intended as the new predecessor.  If this
        is None, or an ID of an object which is not a child of the new parent,
        the entry will be positioned at the end (right) of the ordering.  If
        the empty ID (''), this will be positioned at the front (left) of the
        ordering.  Otherwise, the entry will be given a position_in_parent
        value placing it just after (to the right of) the new predecessor.
    """
    preferred_gap = 2 ** 20

    def ExtendRange(current_limit_entry, sign_multiplier):
      """Compute values at the beginning or end."""
      if current_limit_entry.id_string == entry.id_string:
        step = 0
      else:
        step = sign_multiplier * preferred_gap
      return current_limit_entry.position_in_parent + step

    siblings = [x for x in self._entries.values()
                if x.parent_id_string == parent_id and not x.deleted]
    siblings = sorted(siblings, key=operator.attrgetter('position_in_parent'))
    if prev_id == entry.id_string:
      prev_id = ''
    if not siblings:
      # First item in this container; start in the middle.
      entry.position_in_parent = 0
    elif not prev_id:
      # A special value in the protocol.  Insert at first position.
      entry.position_in_parent = ExtendRange(siblings[0], -1)
    else:
      # Handle mid-insertion; consider items along with their successors.
      for item, successor in zip(siblings, siblings[1:]):
        if item.id_string != prev_id:
          continue
        elif successor.id_string == entry.id_string:
          # We're already in place; don't change anything.
          entry.position_in_parent = successor.position_in_parent
        else:
          # Interpolate new position between the previous item and its
          # existing successor.
          entry.position_in_parent = (item.position_in_parent * 7 +
                                      successor.position_in_parent) / 8
        break
      else:
        # Insert at end. Includes the case where prev_id is None.
        entry.position_in_parent = ExtendRange(siblings[-1], +1)

    entry.parent_id_string = parent_id
    entry.ClearField('insert_after_item_id')

  def _ItemExists(self, id_string):
    """Determine whether an item exists in the changelog."""
    return id_string in self._entries

  def _CreatePermanentItem(self, spec):
    """Create one permanent item from its spec, if it doesn't exist.

    The resulting item is added to the changelog.

    Args:
      spec: A PermanentItem object holding the properties of the item to create.
    """
    id_string = self._ServerTagToId(spec.tag)
    if self._ItemExists(id_string):
      return
    print 'Creating permanent item: %s' % spec.name
    entry = sync_pb2.SyncEntity()
    entry.id_string = id_string
    entry.non_unique_name = spec.name
    entry.name = spec.name
    entry.server_defined_unique_tag = spec.tag
    entry.folder = True
    entry.deleted = False
    entry.specifics.CopyFrom(GetDefaultEntitySpecifics(spec.sync_type))
    self._WritePosition(entry, self._ServerTagToId(spec.parent_tag))
    self._SaveEntry(entry)

  def _CreatePermanentItems(self, requested_types):
    """Ensure creation of all permanent items for a given set of sync types.

    Args:
      requested_types: A list of sync data types from ALL_TYPES.
        Permanent items of only these types will be created.
    """
    for spec in self._PERMANENT_ITEM_SPECS:
      if spec.sync_type in requested_types:
        self._CreatePermanentItem(spec)

  def GetChanges(self, sieve):
    """Get entries which have changed, oldest first.

    The returned entries are limited to being _BATCH_SIZE many.  The entries
    are returned in strict version order.

    Args:
      sieve: An update sieve to use to filter out updates the client
        has already seen.
    Returns:
      A tuple of (version, entries, changes_remaining).  Version is a new
      timestamp value, which should be used as the starting point for the
      next query.  Entries is the batch of entries meeting the current
      timestamp query.  Changes_remaining indicates the number of changes
      left on the server after this batch.
    """
    if not sieve.HasAnyTimestamp():
      return (0, [], 0)
    min_timestamp = sieve.GetMinTimestamp()
    self._CreatePermanentItems(sieve.GetFirstTimeTypes())
    change_log = sorted(self._entries.values(),
                        key=operator.attrgetter('version'))
    new_changes = [x for x in change_log if x.version > min_timestamp]
    # Pick batch_size new changes, and then filter them.  This matches
    # the RPC behavior of the production sync server.
    batch = new_changes[:self._BATCH_SIZE]
    if not batch:
      # Client is up to date.
      return (min_timestamp, [], 0)

    # Restrict batch to requested types.  Tombstones are untyped
    # and will always get included.
    filtered = [copy.deepcopy(item) for item in batch
                if item.deleted or sieve.ClientWantsItem(item)]

    # The new client timestamp is the timestamp of the last item in the
    # batch, even if that item was filtered out.
    return (batch[-1].version, filtered, len(new_changes) - len(batch))

  def _CopyOverImmutableFields(self, entry):
    """Preserve immutable fields by copying pre-commit state.

    Args:
      entry: A sync entity from the client.
    """
    if entry.id_string in self._entries:
      if self._entries[entry.id_string].HasField(
          'server_defined_unique_tag'):
        entry.server_defined_unique_tag = (
            self._entries[entry.id_string].server_defined_unique_tag)

  def _CheckVersionForCommit(self, entry):
    """Perform an optimistic concurrency check on the version number.

    Clients are only allowed to commit if they report having seen the most
    recent version of an object.

    Args:
      entry: A sync entity from the client.  It is assumed that ID fields
        have been converted to server IDs.
    Returns:
      A boolean value indicating whether the client's version matches the
      newest server version for the given entry.
    """
    if entry.id_string in self._entries:
      # Allow edits/deletes if the version matches, and any undeletion.
      return (self._entries[entry.id_string].version == entry.version or
              self._entries[entry.id_string].deleted)
    else:
      # Allow unknown ID only if the client thinks it's new too.
      return entry.version == 0

  def _CheckParentIdForCommit(self, entry):
    """Check that the parent ID referenced in a SyncEntity actually exists.

    Args:
      entry: A sync entity from the client.  It is assumed that ID fields
        have been converted to server IDs.
    Returns:
      A boolean value indicating whether the entity's parent ID is an object
      that actually exists (and is not deleted) in the current account state.
    """
    if entry.parent_id_string == ROOT_ID:
      # This is generally allowed.
      return True
    if entry.parent_id_string not in self._entries:
      print 'Warning: Client sent unknown ID.  Should never happen.'
      return False
    if entry.parent_id_string == entry.id_string:
      print 'Warning: Client sent circular reference.  Should never happen.'
      return False
    if self._entries[entry.parent_id_string].deleted:
      # This can happen in a race condition between two clients.
      return False
    if not self._entries[entry.parent_id_string].folder:
      print 'Warning: Client sent non-folder parent.  Should never happen.'
      return False
    return True

  def _RewriteIdsAsServerIds(self, entry, cache_guid, commit_session):
    """Convert ID fields in a client sync entry to server IDs.

    A commit batch sent by a client may contain new items for which the
    server has not generated IDs yet.  And within a commit batch, later
    items are allowed to refer to earlier items.  This method will
    generate server IDs for new items, as well as rewrite references
    to items whose server IDs were generated earlier in the batch.

    Args:
      entry: The client sync entry to modify.
      cache_guid: The globally unique ID of the client that sent this
        commit request.
      commit_session: A dictionary mapping the original IDs to the new server
        IDs, for any items committed earlier in the batch.
    """
    if entry.version == 0:
      data_type = GetEntryType(entry)
      if entry.HasField('client_defined_unique_tag'):
        # When present, this should determine the item's ID.
        new_id = self._ClientTagToId(data_type, entry.client_defined_unique_tag)
      else:
        new_id = self._ClientIdToId(data_type, cache_guid, entry.id_string)
        entry.originator_cache_guid = cache_guid
        entry.originator_client_item_id = entry.id_string
      commit_session[entry.id_string] = new_id  # Remember the remapping.
      entry.id_string = new_id
    if entry.parent_id_string in commit_session:
      entry.parent_id_string = commit_session[entry.parent_id_string]
    if entry.insert_after_item_id in commit_session:
      entry.insert_after_item_id = commit_session[entry.insert_after_item_id]

  def ValidateCommitEntries(self, entries):
    """Raise an exception if a commit batch contains any global errors.

    Arguments:
      entries: an iterable containing commit-form SyncEntity protocol buffers.

    Raises:
      MigrationDoneError: if any of the entries reference a recently-migrated
        datatype.
    """
    server_ids_in_commit = set()
    local_ids_in_commit = set()
    for entry in entries:
      if entry.version:
        server_ids_in_commit.add(entry.id_string)
      else:
        local_ids_in_commit.add(entry.id_string)
      if entry.HasField('parent_id_string'):
        if entry.parent_id_string not in local_ids_in_commit:
          server_ids_in_commit.add(entry.parent_id_string)

    versions_present = {}
    for server_id in server_ids_in_commit:
      parsed = self._ExtractIdInfo(server_id)
      if parsed:
        datatype, version, _ = parsed
        versions_present.setdefault(datatype, []).append(version)

    self.migration_history.CheckAllCurrent(
         dict((k, min(v)) for k, v in versions_present.iteritems()))

  def CommitEntry(self, entry, cache_guid, commit_session):
    """Attempt to commit one entry to the user's account.

    Args:
      entry: A SyncEntity protobuf representing desired object changes.
      cache_guid: A string value uniquely identifying the client; this
        is used for ID generation and will determine the originator_cache_guid
        if the entry is new.
      commit_session: A dictionary mapping client IDs to server IDs for any
        objects committed earlier this session.  If the entry gets a new ID
        during commit, the change will be recorded here.
    Returns:
      A SyncEntity reflecting the post-commit value of the entry, or None
      if the entry was not committed due to an error.
    """
    entry = copy.deepcopy(entry)

    # Generate server IDs for this entry, and write generated server IDs
    # from earlier entries into the message's fields, as appropriate.  The
    # ID generation state is stored in 'commit_session'.
    self._RewriteIdsAsServerIds(entry, cache_guid, commit_session)

    # Perform the optimistic concurrency check on the entry's version number.
    # Clients are not allowed to commit unless they indicate that they've seen
    # the most recent version of an object.
    if not self._CheckVersionForCommit(entry):
      return None

    # Check the validity of the parent ID; it must exist at this point.
    # TODO(nick): Implement cycle detection and resolution.
    if not self._CheckParentIdForCommit(entry):
      return None

    self._CopyOverImmutableFields(entry);

    # At this point, the commit is definitely going to happen.

    # Deletion works by storing a limited record for an entry, called a
    # tombstone.  A sync server must track deleted IDs forever, since it does
    # not keep track of client knowledge (there's no deletion ACK event).
    if entry.deleted:
      def MakeTombstone(id_string):
        """Make a tombstone entry that will replace the entry being deleted.

        Args:
          id_string: Index of the SyncEntity to be deleted.
        Returns:
          A new SyncEntity reflecting the fact that the entry is deleted.
        """
        # Only the ID, version and deletion state are preserved on a tombstone.
        # TODO(nick): Does the production server not preserve the type?  Not
        # doing so means that tombstones cannot be filtered based on
        # requested_types at GetUpdates time.
        tombstone = sync_pb2.SyncEntity()
        tombstone.id_string = id_string
        tombstone.deleted = True
        tombstone.name = ''
        return tombstone

      def IsChild(child_id):
        """Check if a SyncEntity is a child of entry, or any of its children.

        Args:
          child_id: Index of the SyncEntity that is a possible child of entry.
        Returns:
          True if it is a child; false otherwise.
        """
        if child_id not in self._entries:
          return False
        if self._entries[child_id].parent_id_string == entry.id_string:
          return True
        return IsChild(self._entries[child_id].parent_id_string)

      # Identify any children entry might have.
      child_ids = [child.id_string for child in self._entries.itervalues()
                   if IsChild(child.id_string)]

      # Mark all children that were identified as deleted.
      for child_id in child_ids:
        self._SaveEntry(MakeTombstone(child_id))

      # Delete entry itself.
      entry = MakeTombstone(entry.id_string)
    else:
      # Comments in sync.proto detail how the representation of positional
      # ordering works: the 'insert_after_item_id' field specifies a
      # predecessor during Commit operations, but the 'position_in_parent'
      # field provides an absolute ordering in GetUpdates contexts.  Here
      # we convert from the former to the latter.  Specifically, we'll
      # generate a numeric position placing the item just after the object
      # identified by 'insert_after_item_id', and then clear the
      # 'insert_after_item_id' field so that it's not sent back to the client
      # during later GetUpdates requests.
      if entry.HasField('insert_after_item_id'):
        self._WritePosition(entry, entry.parent_id_string,
                            entry.insert_after_item_id)
      else:
        self._WritePosition(entry, entry.parent_id_string)

    # Preserve the originator info, which the client is not required to send
    # when updating.
    base_entry = self._entries.get(entry.id_string)
    if base_entry and not entry.HasField('originator_cache_guid'):
      entry.originator_cache_guid = base_entry.originator_cache_guid
      entry.originator_client_item_id = base_entry.originator_client_item_id

    # Commit the change.  This also updates the version number.
    self._SaveEntry(entry)
    return entry

  def _RewriteVersionInId(self, id_string):
    """Rewrites an ID so that its migration version becomes current."""
    parsed_id = self._ExtractIdInfo(id_string)
    if not parsed_id:
      return id_string
    datatype, old_migration_version, inner_id = parsed_id
    return self._MakeCurrentId(datatype, inner_id)

  def TriggerMigration(self, datatypes):
    """Cause a migration to occur for a set of datatypes on this account.

    Clients will see the MIGRATION_DONE error for these datatypes until they
    resync them.
    """
    versions_to_remap = self.migration_history.Bump(datatypes)
    all_entries = self._entries.values()
    self._entries.clear()
    for entry in all_entries:
      new_id = self._RewriteVersionInId(entry.id_string)
      entry.id_string = new_id
      if entry.HasField('parent_id_string'):
        entry.parent_id_string = self._RewriteVersionInId(
            entry.parent_id_string)
      self._entries[entry.id_string] = entry

class TestServer(object):
  """An object to handle requests for one (and only one) Chrome Sync account.

  TestServer consumes the sync command messages that are the outermost
  layers of the protocol, performs the corresponding actions on its
  SyncDataModel, and constructs an appropropriate response message.
  """

  def __init__(self):
    # The implementation supports exactly one account; its state is here.
    self.account = SyncDataModel()
    self.account_lock = threading.Lock()
    # Clients that have talked to us: a map from the full client ID
    # to its nickname.
    self.clients = {}
    self.client_name_generator = ('+' * times + chr(c)
        for times in xrange(0, sys.maxint) for c in xrange(ord('A'), ord('Z')))

  def GetShortClientName(self, query):
    parsed = cgi.parse_qs(query[query.find('?')+1:])
    client_id = parsed.get('client_id')
    if not client_id:
      return '?'
    client_id = client_id[0]
    if client_id not in self.clients:
      self.clients[client_id] = self.client_name_generator.next()
    return self.clients[client_id]

  def CheckStoreBirthday(self, request):
    """Raises StoreBirthdayError if the request's birthday is a mismatch."""
    if not request.HasField('store_birthday'):
      return
    if self.account.store_birthday != request.store_birthday:
      raise StoreBirthdayError

  def HandleMigrate(self, path):
    query = urlparse.urlparse(path)[4]
    code = 200
    self.account_lock.acquire()
    try:
      datatypes = [DataTypeStringToSyncTypeLoose(x)
                   for x in urlparse.parse_qs(query).get('type',[])]
      if datatypes:
        self.account.TriggerMigration(datatypes)
        response = 'Migrated datatypes %s' % (
            ' and '.join(SyncTypeToString(x).upper() for x in datatypes))
      else:
        response = 'Please specify one or more <i>type=name</i> parameters'
        code = 400
    except DataTypeIdNotRecognized, error:
      response = 'Could not interpret datatype name'
      code = 400
    finally:
      self.account_lock.release()
    return (code, '<html><title>Migration: %d</title><H1>%d %s</H1></html>' %
                (code, code, response))

  def HandleCommand(self, query, raw_request):
    """Decode and handle a sync command from a raw input of bytes.

    This is the main entry point for this class.  It is safe to call this
    method from multiple threads.

    Args:
      raw_request: An iterable byte sequence to be interpreted as a sync
        protocol command.
    Returns:
      A tuple (response_code, raw_response); the first value is an HTTP
      result code, while the second value is a string of bytes which is the
      serialized reply to the command.
    """
    self.account_lock.acquire()
    def print_context(direction):
      print '[Client %s %s %s.py]' % (self.GetShortClientName(query), direction,
                                      __name__),

    try:
      request = sync_pb2.ClientToServerMessage()
      request.MergeFromString(raw_request)
      contents = request.message_contents

      response = sync_pb2.ClientToServerResponse()
      response.error_code = sync_pb2.ClientToServerResponse.SUCCESS
      self.CheckStoreBirthday(request)
      response.store_birthday = self.account.store_birthday

      print_context('->')

      if contents == sync_pb2.ClientToServerMessage.AUTHENTICATE:
        print 'Authenticate'
        # We accept any authentication token, and support only one account.
        # TODO(nick): Mock out the GAIA authentication as well; hook up here.
        response.authenticate.user.email = 'syncjuser@chromium'
        response.authenticate.user.display_name = 'Sync J User'
      elif contents == sync_pb2.ClientToServerMessage.COMMIT:
        print 'Commit %d item(s)' % len(request.commit.entries)
        self.HandleCommit(request.commit, response.commit)
      elif contents == sync_pb2.ClientToServerMessage.GET_UPDATES:
        print 'GetUpdates',
        self.HandleGetUpdates(request.get_updates, response.get_updates)
        print_context('<-')
        print '%d update(s)' % len(response.get_updates.entries)
      else:
        print 'Unrecognizable sync request!'
        return (400, None)  # Bad request.
      return (200, response.SerializeToString())
    except MigrationDoneError as error:
      print_context('<-')
      print 'MIGRATION_DONE: <%s>' % (ShortDatatypeListSummary(error.datatypes))
      response = sync_pb2.ClientToServerResponse()
      response.store_birthday = self.account.store_birthday
      response.error_code = sync_pb2.ClientToServerResponse.MIGRATION_DONE
      response.migrated_data_type_id[:] = [
          SyncTypeToProtocolDataTypeId(x) for x in error.datatypes]
      return (200, response.SerializeToString())
    except StoreBirthdayError as error:
      print_context('<-')
      print 'NOT_MY_BIRTHDAY'
      response = sync_pb2.ClientToServerResponse()
      response.store_birthday = self.account.store_birthday
      response.error_code = sync_pb2.ClientToServerResponse.NOT_MY_BIRTHDAY
      return (200, response.SerializeToString())
    finally:
      self.account_lock.release()

  def HandleCommit(self, commit_message, commit_response):
    """Respond to a Commit request by updating the user's account state.

    Commit attempts stop after the first error, returning a CONFLICT result
    for any unattempted entries.

    Args:
      commit_message: A sync_pb.CommitMessage protobuf holding the content
        of the client's request.
      commit_response: A sync_pb.CommitResponse protobuf into which a reply
        to the client request will be written.
    """
    commit_response.SetInParent()
    batch_failure = False
    session = {}  # Tracks ID renaming during the commit operation.
    guid = commit_message.cache_guid

    self.account.ValidateCommitEntries(commit_message.entries)

    for entry in commit_message.entries:
      server_entry = None
      if not batch_failure:
        # Try to commit the change to the account.
        server_entry = self.account.CommitEntry(entry, guid, session)

      # An entryresponse is returned in both success and failure cases.
      reply = commit_response.entryresponse.add()
      if not server_entry:
        reply.response_type = sync_pb2.CommitResponse.CONFLICT
        reply.error_message = 'Conflict.'
        batch_failure = True  # One failure halts the batch.
      else:
        reply.response_type = sync_pb2.CommitResponse.SUCCESS
        # These are the properties that the server is allowed to override
        # during commit; the client wants to know their values at the end
        # of the operation.
        reply.id_string = server_entry.id_string
        if not server_entry.deleted:
          # Note: the production server doesn't actually send the
          # parent_id_string on commit responses, so we don't either.
          reply.position_in_parent = server_entry.position_in_parent
          reply.version = server_entry.version
          reply.name = server_entry.name
          reply.non_unique_name = server_entry.non_unique_name
        else:
          reply.version = entry.version + 1

  def HandleGetUpdates(self, update_request, update_response):
    """Respond to a GetUpdates request by querying the user's account.

    Args:
      update_request: A sync_pb.GetUpdatesMessage protobuf holding the content
        of the client's request.
      update_response: A sync_pb.GetUpdatesResponse protobuf into which a reply
        to the client request will be written.
    """
    update_response.SetInParent()
    update_sieve = UpdateSieve(update_request, self.account.migration_history)

    print CallerInfoToString(update_request.caller_info.source),
    print update_sieve.SummarizeRequest()

    update_sieve.CheckMigrationState()

    new_timestamp, entries, remaining = self.account.GetChanges(update_sieve)

    update_response.changes_remaining = remaining
    for entry in entries:
      reply = update_response.entries.add()
      reply.CopyFrom(entry)
    update_sieve.SaveProgress(new_timestamp, update_response)


text:
#include "DumbNet/dn_server.h"

void sigchld_handler(int s)
{
    while(waitpid(-1, NULL, WNOHANG) > 0);
}

void serv_connec_proc ( dn_socket sockfd /*listening socket*/
                            , dn_socket new_fd /*comm socket */
                        )
{
        // this is the child process
        close(sockfd); // child doesn't need the listener
        if (send(new_fd, "Hello, world!", 13, 0) == -1)
            perror("send");
        close(new_fd);
        exit(0);
}

void serv_connec_accept (dn_socket sockfd /*listening socket*/
                            ,void (*serv_connec_proc) (dn_socket, dn_socket)
                        )
{
        dn_socket new_fd;
        socklen_t sin_size;
        struct sockaddr_storage their_addr; // connector's address information
        char s[INET6_ADDRSTRLEN];

        sin_size = sizeof their_addr;
        new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
        if (new_fd == -1) {
            perror("accept");
            return;
        }

        inet_ntop(their_addr.ss_family,
            get_in_addr((struct sockaddr *)&their_addr),
            s, sizeof s);
        printf("server: got connection from %s\n", s);

        if (!fork()) { // this is the child process
            serv_connec_proc(sockfd,new_fd);
        }
        close(new_fd);  // parent doesn't need this
}

/* return listening socket */
dn_socket serv_init()
{
     int sockfd;//, new_fd;  // listen on sock_fd, new connection on new_fd
    struct addrinfo hints, *servinfo, *p;
    //struct sockaddr_storage their_addr; // connector's address information
    //socklen_t sin_size;
    struct sigaction sa;
    int yes=1;
    //char s[INET6_ADDRSTRLEN];
    int rv;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }

        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
                sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }

        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server: bind");
            continue;
        }

        break;
    }

    if (p == NULL)  {
        fprintf(stderr, "server: failed to bind\n");
        return 2;
    }

    freeaddrinfo(servinfo); // all done with this structure

    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    sa.sa_handler = sigchld_handler; // reap all dead processes
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    printf("server: waiting for connections...\n");
    return sockfd;
}

redpajama_set_name: RedPajamaGithub


text:
package org.jkiss.dbeaver.ext.oracle.model.lock;

import org.jkiss.dbeaver.model.admin.locks.DBAServerLockItem;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.meta.Property;

import java.sql.ResultSet;

public class OracleLockItem implements DBAServerLockItem{

	private String lockType;
	private String modeHeld;
	private String modeRequest;
	private String ids;
	private Integer lastConvert;
	private String blocking;

	public OracleLockItem(ResultSet dbResult) {

     this.lockType = JDBCUtils.safeGetString(dbResult, "lock_type");
     this.modeHeld = JDBCUtils.safeGetString(dbResult, "mode_held");
     this.modeRequest = JDBCUtils.safeGetString(dbResult, "mode_requested");
     StringBuilder sb = new StringBuilder(String.valueOf(JDBCUtils.safeGetLong(dbResult, "lock_id1")));
	 sb.append("/");
	 sb.append(String.valueOf(JDBCUtils.safeGetLong(dbResult, "lock_id2")));
	 this.ids = sb.toString();
     this.lastConvert = JDBCUtils.safeGetInt(dbResult, "last_convert");
     this.blocking = JDBCUtils.safeGetString(dbResult, "blocking_others");

    }

	@Property(viewable = true, order = 1)
	public String getLockType()
	{
		return lockType;
	}

	@Property(viewable = true, order = 2)
	public String getModeHeld()
	{
		return modeHeld;
	}

	@Property(viewable = true, order = 3)
	public String getModeRequest()
	{
		return modeRequest;
	}

	@Property(viewable = true, order = 4)
	public String getIds()
	{
		return ids;
	}

	@Property(viewable = true, order = 5)
	public Integer getLastConvert()
	{
		return lastConvert;
	}

	@Property(viewable = true, order = 6)
	public String getBlocking()
	{
		return blocking;
	}

}


text:
"""
============
obiwan
============

A package for injecting artificial stars and galaxy-targets (ELGs, LRGs, and QSOs) into the individual images of the DESI Legacy Surveys, DECALS, MzLS, and BASS. The legacypipe/Tractor pipeline is rerun on the modified image data, and the resulting *simulated* Tractor catalogues form a Data Release (DR) resembling a DR of real objects.
"""
#
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
# The line above will help with 2to3 support.
#
# Set version string.
#
from ._version import __version__


text:
class Groups::DeployTokensController < Groups::ApplicationController
  before_action :authorize_destroy_deploy_token!

  def revoke
    @token = @group.deploy_tokens.find(params[:id])
    @token.revoke!

    redirect_to group_settings_repository_path(@group, anchor: 'js-deploy-tokens')
  end
end


text:
"""
This module implements functions and classes for spatial statistics.
"""

import numpy as np
import math

class RipleysKEstimator:
    """
    Estimators for Ripley's K function for two-dimensional spatial data.
    See [1]_, [2]_, [3]_, [4]_, [5]_ for detailed mathematical and
    practical aspects of those estimators.

    Parameters
    ----------
    area : float
        Area of study from which the points where observed.
    x_max, y_max : float, float, optional
        Maximum rectangular coordinates of the area of study.
        Required if ``mode == 'translation'`` or ``mode == ohser``.
    x_min, y_min : float, float, optional
        Minimum rectangular coordinates of the area of study.
        Required if ``mode == 'variable-width'`` or ``mode == ohser``.

    Examples
    --------
    >>> import numpy as np
    >>> from matplotlib import pyplot as plt # doctest: +SKIP
    >>> from astropy.stats import RipleysKEstimator
    >>> z = np.random.uniform(low=5, high=10, size=(100, 2))
    >>> Kest = RipleysKEstimator(area=25, x_max=10, y_max=10,
    ... x_min=5, y_min=5)
    >>> r = np.linspace(0, 2.5, 100)
    >>> plt.plot(r, Kest.poisson(r)) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='none')) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='translation')) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='ohser')) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='var-width')) # doctest: +SKIP
    >>> plt.plot(r, Kest(data=z, radii=r, mode='ripley')) # doctest: +SKIP

    References
    ----------
    .. [1] Peebles, P.J.E. *The large scale structure of the universe*.
       <http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=1980lssu.book.....P&db_key=AST>
    .. [2] Spatial descriptive statistics.
       <https://en.wikipedia.org/wiki/Spatial_descriptive_statistics>
    .. [3] Package spatstat.
       <https://cran.r-project.org/web/packages/spatstat/spatstat.pdf>
    .. [4] Cressie, N.A.C. (1991). Statistics for Spatial Data,
       Wiley, New York.
    .. [5] Stoyan, D., Stoyan, H. (1992). Fractals, Random Shapes and
       Point Fields, Akademie Verlag GmbH, Chichester.
    """

    def __init__(self, area, x_max=None, y_max=None, x_min=None, y_min=None):
        self.area = area
        self.x_max = x_max
        self.y_max = y_max
        self.x_min = x_min
        self.y_min = y_min

    @property
    def area(self):
        return self._area

    @area.setter
    def area(self, value):
        if isinstance(value, (float, int)) and value > 0:
            self._area = value
        else:
            raise ValueError('area is expected to be a positive number. '
                             'Got {}.'.format(value))

    @property
    def y_max(self):
        return self._y_max

    @y_max.setter
    def y_max(self, value):
        if value is None or isinstance(value, (float, int)):
            self._y_max = value
        else:
            raise ValueError('y_max is expected to be a real number '
                             'or None. Got {}.'.format(value))

    @property
    def x_max(self):
        return self._x_max

    @x_max.setter
    def x_max(self, value):
        if value is None or isinstance(value, (float, int)):
            self._x_max = value
        else:
            raise ValueError('x_max is expected to be a real number '
                             'or None. Got {}.'.format(value))

    @property
    def y_min(self):
        return self._y_min

    @y_min.setter
    def y_min(self, value):
        if value is None or isinstance(value, (float, int)):
            self._y_min = value
        else:
            raise ValueError('y_min is expected to be a real number. '
                             'Got {}.'.format(value))

    @property
    def x_min(self):
        return self._x_min

    @x_min.setter
    def x_min(self, value):
        if value is None or isinstance(value, (float, int)):
            self._x_min = value
        else:
            raise ValueError('x_min is expected to be a real number. '
                             'Got {}.'.format(value))

    def __call__(self, data, radii, mode='none'):
        return self.evaluate(data=data, radii=radii, mode=mode)

    def _pairwise_diffs(self, data):
        npts = len(data)
        diff = np.zeros(shape=(npts * (npts - 1) // 2, 2), dtype=np.double)
        k = 0
        for i in range(npts - 1):
            size = npts - i - 1
            diff[k:k + size] = abs(data[i] - data[i+1:])
            k += size

        return diff

    def poisson(self, radii):
        """
        Evaluates the Ripley K function for the homogeneous Poisson process,
        also known as Complete State of Randomness (CSR).

        Parameters
        ----------
        radii : 1D array
            Set of distances in which Ripley's K function will be evaluated.

        Returns
        -------
        output : 1D array
            Ripley's K function evaluated at ``radii``.
        """

        return np.pi * radii * radii

    def Lfunction(self, data, radii, mode='none'):
        """
        Evaluates the L function at ``radii``. For parameter description
        see ``evaluate`` method.
        """

        return np.sqrt(self.evaluate(data, radii, mode=mode) / np.pi)

    def Hfunction(self, data, radii, mode='none'):
        """
        Evaluates the H function at ``radii``. For parameter description
        see ``evaluate`` method.
        """

        return self.Lfunction(data, radii, mode=mode) - radii

    def evaluate(self, data, radii, mode='none'):
        """
        Evaluates the Ripley K estimator for a given set of values ``radii``.

        Parameters
        ----------
        data : 2D array
            Set of observed points in as a n by 2 array which will be used to
            estimate Ripley's K function.
        radii : 1D array
            Set of distances in which Ripley's K estimator will be evaluated.
            Usually, it's common to consider max(radii) < (area/2)**0.5.
        mode : str
            Keyword which indicates the method for edge effects correction.
            Available methods are 'none', 'translation', 'ohser', 'var-width',
            and 'ripley'.

            * 'none'
                this method does not take into account any edge effects
                whatsoever.
            * 'translation'
                computes the intersection of rectangular areas centered at
                the given points provided the upper bounds of the
                dimensions of the rectangular area of study. It assumes that
                all the points lie in a bounded rectangular region satisfying
                x_min < x_i < x_max; y_min < y_i < y_max. A detailed
                description of this method can be found on ref [4].
            * 'ohser'
                this method uses the isotropized set covariance function of
                the window of study as a weight to correct for
                edge-effects. A detailed description of this method can be
                found on ref [4].
            * 'var-width'
                this method considers the distance of each observed point to
                the nearest boundary of the study window as a factor to
                account for edge-effects. See [3] for a brief description of
                this method.
            * 'ripley'
                this method is known as Ripley's edge-corrected estimator.
                The weight for edge-correction is a function of the
                proportions of circumferences centered at each data point
                which crosses another data point of interest. See [3] for
                a detailed description of this method.

        Returns
        -------
        ripley : 1D array
            Ripley's K function estimator evaluated at ``radii``.
        """

        data = np.asarray(data)

        if not data.shape[1] == 2:
            raise ValueError('data must be an n by 2 array, where n is the '
                             'number of observed points.')

        npts = len(data)
        ripley = np.zeros(len(radii))

        if mode == 'none':
            diff = self._pairwise_diffs(data)
            distances = np.hypot(diff[:, 0], diff[:, 1])
            for r in range(len(radii)):
                ripley[r] = (distances < radii[r]).sum()

            ripley = self.area * 2. * ripley / (npts * (npts - 1))
        # eq. 15.11 Stoyan book page 283
        elif mode == 'translation':
            diff = self._pairwise_diffs(data)
            distances = np.hypot(diff[:, 0], diff[:, 1])
            intersec_area = (((self.x_max - self.x_min) - diff[:, 0]) *
                             ((self.y_max - self.y_min) - diff[:, 1]))

            for r in range(len(radii)):
                dist_indicator = distances < radii[r]
                ripley[r] = ((1 / intersec_area) * dist_indicator).sum()

            ripley = (self.area**2 / (npts * (npts - 1))) * 2 * ripley
        # Stoyan book page 123 and eq 15.13
        elif mode == 'ohser':
            diff = self._pairwise_diffs(data)
            distances = np.hypot(diff[:, 0], diff[:, 1])
            a = self.area
            b = max((self.y_max - self.y_min) / (self.x_max - self.x_min),
                    (self.x_max - self.x_min) / (self.y_max - self.y_min))
            x = distances / math.sqrt(a / b)
            u = np.sqrt((x * x - 1) * (x > 1))
            v = np.sqrt((x * x - b ** 2) * (x < math.sqrt(b ** 2 + 1)) * (x > b))
            c1 = np.pi - 2 * x * (1 + 1 / b) + x * x / b
            c2 = 2 * np.arcsin((1 / x) * (x > 1)) - 1 / b - 2 * (x - u)
            c3 = (2 * np.arcsin(((b - u * v) / (x * x))
                                * (x > b) * (x < math.sqrt(b ** 2 + 1)))
                  + 2 * u + 2 * v / b - b - (1 + x * x) / b)

            cov_func = ((a / np.pi) * (c1 * (x >= 0) * (x <= 1)
                        + c2 * (x > 1) * (x <= b)
                        + c3 * (b < x) * (x < math.sqrt(b ** 2 + 1))))

            for r in range(len(radii)):
                dist_indicator = distances < radii[r]
                ripley[r] = ((1 / cov_func) * dist_indicator).sum()

            ripley = (self.area**2 / (npts * (npts - 1))) * 2 * ripley
        # Cressie book eq 8.2.20 page 616
        elif mode == 'var-width':
            lt_dist = np.minimum(np.minimum(self.x_max - data[:, 0], self.y_max - data[:, 1]),
                                 np.minimum(data[:, 0] - self.x_min, data[:, 1] - self.y_min))

            for r in range(len(radii)):
                for i in range(npts):
                    for j in range(npts):
                        if i != j:
                            diff = abs(data[i] - data[j])
                            dist = math.sqrt((diff * diff).sum())
                            if dist < radii[r] < lt_dist[i]:
                                ripley[r] = ripley[r] + 1
                lt_dist_sum = (lt_dist > radii[r]).sum()
                if not lt_dist_sum == 0:
                    ripley[r] = ripley[r] / lt_dist_sum

            ripley = self.area * ripley / npts
        # Cressie book eq 8.4.22 page 640
        elif mode == 'ripley':
            hor_dist = np.zeros(shape=(npts * (npts - 1)) // 2,
                                dtype=np.double)
            ver_dist = np.zeros(shape=(npts * (npts - 1)) // 2,
                                dtype=np.double)

            for k in range(npts - 1):
                min_hor_dist = min(self.x_max - data[k][0],
                                   data[k][0] - self.x_min)
                min_ver_dist = min(self.y_max - data[k][1],
                                   data[k][1] - self.y_min)
                start = (k * (2 * (npts - 1) - (k - 1))) // 2
                end = ((k + 1) * (2 * (npts - 1) - k)) // 2
                hor_dist[start: end] = min_hor_dist * np.ones(npts - 1 - k)
                ver_dist[start: end] = min_ver_dist * np.ones(npts - 1 - k)

            diff = self._pairwise_diffs(data)
            dist = np.hypot(diff[:, 0], diff[:, 1])
            dist_ind = dist <= np.hypot(hor_dist, ver_dist)

            w1 = (1 - (np.arccos(np.minimum(ver_dist, dist) / dist) +
                       np.arccos(np.minimum(hor_dist, dist) / dist)) / np.pi)
            w2 = (3 / 4 - 0.5 * (
                np.arccos(ver_dist / dist * ~dist_ind) +
                np.arccos(hor_dist / dist * ~dist_ind)) / np.pi)

            weight = dist_ind * w1 + ~dist_ind * w2

            for r in range(len(radii)):
                ripley[r] = ((dist < radii[r]) / weight).sum()

            ripley = self.area * 2. * ripley / (npts * (npts - 1))
        else:
            raise ValueError(f'mode {mode} is not implemented.')

        return ripley


text:
using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(RoarLeaderboardsModule))]
public class RoarLeaderboardsModuleInspector : RoarModuleInspector
{
	private SerializedProperty whenToFetch;
	private SerializedProperty howOftenToFetch;

	private SerializedProperty scrollViewHeight;
	private SerializedProperty scrollBarPadding;
	private SerializedProperty leaderboardEntryWidth;
	private SerializedProperty leaderboardEntryHeight;
	private SerializedProperty leaderboardEntrySpacing;
	private SerializedProperty leaderboardEntryStyle;
	private SerializedProperty leaderboardTitleRect;
	private SerializedProperty leaderboardTitleOnRankingStyle;
	private SerializedProperty leaderboardTitlePadding;
	private SerializedProperty rankingEntryWidth;
	private SerializedProperty rankingEntryHeight;
	private SerializedProperty rankingEntrySpacing;
	private SerializedProperty rankingEntryPlayerRankStyle;
	private SerializedProperty rankingEntryPlayerNameStyle;
	private SerializedProperty rankingEntryPlayerScoreStyle;

	protected override void OnEnable()
	{
		base.OnEnable();
		showBoundsConstraintSettings = false;

		whenToFetch = serializedObject.FindProperty("whenToFetch");
		howOftenToFetch = serializedObject.FindProperty("howOftenToFetch");

		scrollViewHeight = serializedObject.FindProperty("scrollViewHeight");
		scrollBarPadding = serializedObject.FindProperty("scrollBarPadding");
		leaderboardEntryWidth = serializedObject.FindProperty("leaderboardEntryWidth");
		leaderboardEntryHeight = serializedObject.FindProperty("leaderboardEntryHeight");
		leaderboardEntrySpacing = serializedObject.FindProperty("leaderboardEntrySpacing");
		leaderboardEntryStyle = serializedObject.FindProperty("leaderboardEntryStyle");

		leaderboardTitleRect = serializedObject.FindProperty("leaderboardTitleRect");
		leaderboardTitleOnRankingStyle = serializedObject.FindProperty("leaderboardTitleOnRankingStyle");
		leaderboardTitlePadding = serializedObject.FindProperty("leaderboardTitlePadding");

		rankingEntryWidth = serializedObject.FindProperty("rankingEntryWidth");
		rankingEntryHeight = serializedObject.FindProperty("rankingEntryHeight");
		rankingEntrySpacing = serializedObject.FindProperty("rankingEntrySpacing");
		rankingEntryPlayerRankStyle = serializedObject.FindProperty("rankingEntryPlayerRankStyle");
		rankingEntryPlayerNameStyle = serializedObject.FindProperty("rankingEntryPlayerNameStyle");
		rankingEntryPlayerScoreStyle = serializedObject.FindProperty("rankingEntryPlayerScoreStyle");
	}

	protected override void DrawGUI()
	{
		base.DrawGUI();

		// data fetching
		Comment("How often to fetch leaderboard data from the server.");
		EditorGUILayout.PropertyField(whenToFetch);
		if (whenToFetch.enumValueIndex == 2)
			EditorGUILayout.PropertyField(howOftenToFetch, new GUIContent("How Often (seconds)"));

		Comment("Miscellaneous attributes.");
		EditorGUILayout.PropertyField(scrollViewHeight);
		EditorGUILayout.PropertyField(scrollBarPadding);
		EditorGUILayout.PropertyField(leaderboardEntryWidth);
		EditorGUILayout.PropertyField(leaderboardEntryHeight);
		EditorGUILayout.PropertyField(leaderboardEntrySpacing);
		EditorGUILayout.PropertyField(leaderboardEntryStyle);

		EditorGUILayout.PropertyField(leaderboardTitleRect);
		EditorGUILayout.PropertyField(leaderboardTitleOnRankingStyle);
		EditorGUILayout.PropertyField(leaderboardTitlePadding);

		EditorGUILayout.PropertyField(rankingEntryWidth);
		EditorGUILayout.PropertyField(rankingEntryHeight);
		EditorGUILayout.PropertyField(rankingEntrySpacing);
		EditorGUILayout.PropertyField(rankingEntryPlayerRankStyle);
		EditorGUILayout.PropertyField(rankingEntryPlayerNameStyle);
		EditorGUILayout.PropertyField(rankingEntryPlayerScoreStyle);
	}
}


text:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include "allocator.h"
#include "comparator.h"

#include <cstddef>

// TODO: Check if the iterators and entry class can be nested into the LinkedList class.

namespace ecpp {

// This prototype is required to make sure LinkedList can be used in the definition of LinkedListIterator and LinkedListEntry.
template <typename T>
class LinkedList;

// This prototype is required to make sure LinkedListIterator can be used in the definition of LinkedListEntry.
template <typename T>
class LinkedListIterator;

// This prototype is required to make sure ConstLinkedListIterator can be used in the definition of LinkedListEntry.
template <typename T>
class ConstLinkedListIterator;

template <typename T>
class LinkedListEntry {
public:
    static constexpr std::size_t LINKED_LIST_ENTRY_SIZE = sizeof(LinkedListEntry<T>);

    LinkedListEntry(T item) : _item(item), _next(nullptr), _previous(nullptr) {
    }

private:
    T _item;
    LinkedListEntry *_next;
    LinkedListEntry *_previous;

    friend class LinkedList<T>;
    friend class LinkedListIterator<T>;
    friend class ConstLinkedListIterator<T>;
};

template <typename T>
class LinkedListIterator {
public:
    LinkedListIterator(LinkedList<T> &list, LinkedListEntry<T> *entry) : _list(list), _entry(entry) {
    }

    bool insertAfter(T item) {

        // This protects against calling insertAfter on null iterator of the linked list.
        if ((_entry == nullptr) && (_list._head != nullptr)) {
            return false;
        }

        // make the new entry
        LinkedListEntry<T> *newEntry = makeEntry(item);
        if (newEntry == nullptr) {
            return false;
        }

        // handle linking
        if (_entry != nullptr) {

            /*
             * This branch deals with updating a non-empty list.
             */

            newEntry->_previous = _entry;

            if (_entry->_next != nullptr) {
                _entry->_next->_previous = newEntry;
            } else {
                // If there was no next entry, we have added at the end of the list and we must update the tail.
                _list._tail = newEntry;
            }

            _entry->_next = newEntry;

        } else {

            /*
             * This branch deals with starting an empty list.
             */

            _list._head = newEntry;
            _list._tail = newEntry;
        }

        // Point to the newly created entry.
        _entry = newEntry;

        _list._size++;

        return true;
    }

    bool insertBefore(T item) {

        // This protects against calling insertAfter on null iterator of the linked list.
        if ((_entry == nullptr) && (_list._head != nullptr)) {
            return false;
        }

        // make the new entry
        LinkedListEntry<T> *newEntry = makeEntry(item);
        if (newEntry == nullptr) {
            return false;
        }

        // handle linking
        if (_entry != nullptr) {

            /*
             * This branch deals with updating a non-empty list.
             */

            newEntry->_next = _entry;

            if (_entry->_previous != nullptr) {
                _entry->_previous->_next = newEntry;
            } else {
                // If there was no previous entry, we have inserted at the start of the list and must update the head.
                _list._head = newEntry;
            }

            _entry->_previous = newEntry;

        } else {

            /*
             * This branch deals with starting an empty list.
             */

            _list._head = newEntry;
            _list._tail = newEntry;
        }

        // Point to the newly created entry.
        _entry = newEntry;

        _list._size++;

        return true;
    }

    bool remove() {

        if (_entry == nullptr) {
            return false;
        }

        // Find out what to do.
        bool hasPrevious = _entry->_previous != nullptr;
        bool hasNext = _entry->_next != nullptr;

        if ((hasPrevious == true) && (hasNext == true)) {

            // Both next and previous are present, link together.
            _entry->_previous->_next = _entry->_next;
            _entry->_next->_previous = _entry->_previous;

        } else if (hasPrevious == true) {

            // Only previous is present, link _previous->_next to nullptr. We must also adjust the tail of the list.
            _entry->_previous->_next = nullptr;
            _list._tail = _entry->_previous;

        } else if (hasNext == true) {

            // Only next is present, link _next->_previous to nullptr. We must also adjust the head of the list.
            _entry->_next->_previous = nullptr;
            _list._head = _entry->_next;
        }

        // De-allocate the entry and point to the next entry if there is one.
        LinkedListEntry<T> *target = (hasNext == true) ? _entry->_next : nullptr;
        _list._allocator.deallocate(_entry);
        _entry = target;
        --_list._size;

        return true;
    }

    LinkedListIterator<T> &operator ++() {
        if (_entry != nullptr) {
            _entry = _entry->_next;
        }
        return *this;
    }

    LinkedListIterator<T> &operator --() {
        if (_entry != nullptr) {
            _entry = _entry->_previous;
        }
        return *this;
    }

    bool operator !=(const LinkedListIterator<T> &other) const {
        if (other._entry != nullptr) {
            return _entry != other._entry->_next;
        } else {
            return _entry != other._entry;
        }
    }

    T operator *() {
        if (_entry == nullptr) {
            return T();
        } else {
            return _entry->_item;
        }
    }

private:
    LinkedListEntry<T> *makeEntry(T item) {

        // Allocate memory for the new entry instance.
        void * mem = _list._allocator.allocate(LinkedListEntry<T>::LINKED_LIST_ENTRY_SIZE);

        // If no memory available return the nullptr.
        if (mem == nullptr) {
            return nullptr;
        }

        // Call constructor for new entry by using placement new.
        LinkedListEntry<T> *newEntry = new (mem) LinkedListEntry<T>(item);

        return newEntry;
    }

    LinkedList<T> &_list;
    LinkedListEntry<T> *_entry;
};

template <typename T>
class ConstLinkedListIterator {
public:
    ConstLinkedListIterator(LinkedListEntry<T> *entry) : _entry(entry) {
    }

    ConstLinkedListIterator<T> &operator ++() {
        if (_entry != nullptr) {
            _entry = _entry->_next;
        }
        return *this;
    }

    ConstLinkedListIterator<T> &operator --() {
        if (_entry != nullptr) {
            _entry = _entry->_previous;
        }
        return *this;
    }

    bool operator !=(const ConstLinkedListIterator<T> &other) const {
        if (other._entry != nullptr) {
            return _entry != other._entry->_next;
        } else {
            return _entry != other._entry;
        }
    }

    const T operator *() const {
        if (_entry == nullptr) {
            return T();
        } else {
            return _entry->_item;
        }
    }

private:
    LinkedListEntry<T> *_entry;
};

template <typename T>
class LinkedList {
public:
    LinkedList(Allocator &allocator) : _allocator(allocator), _head(nullptr), _tail(nullptr), _size(0) {
    }

    std::size_t size() const {
        return _size;
    }

    bool append(T item) {
        return end().insertAfter(item);
    }

    bool prepend(T item) {
        return begin().insertBefore(item);
    }

    const T at(const std::size_t pos) const {
        LinkedListEntry<T> *entry = findEntry(pos);
        if (entry == nullptr) {
            return T();
        } else {
            return entry->_item;
        }
    }

    void clear() {
        while(_size != 0) {
            begin().remove();
        }
    }

    bool swap(const std::size_t fromPos, const std::size_t toPos) {

        LinkedListEntry<T> *from = findEntry(fromPos);
        LinkedListEntry<T> *to = findEntry(toPos);

        if ((from == nullptr) || (to == nullptr)) {
            return false;
        }

        bool fromWasHead = (from == _head);
        bool fromWasTail = (from == _tail);
        bool toWasHead = (to == _head);
        bool toWasTail = (to == _tail);

        LinkedListEntry<T> *fromNext = from->_next;
        LinkedListEntry<T> *fromPrevious = from->_previous;
        LinkedListEntry<T> *toNext = to->_next;
        LinkedListEntry<T> *toPrevious = to->_previous;

        // Update next and previous of 'from' to point to 'to'.
        if (fromPrevious != nullptr) {
            fromPrevious->_next = to;
        }
        if (fromNext != nullptr) {
            fromNext->_previous = to;
        }

        // Update 'to' 's next and previous to point to what 'from' was pointing to.
        to->_next = fromNext;
        to->_previous = fromPrevious;

        // Update next and previous of 'to' to point to 'from'.
        if (toPrevious != nullptr) {
            toPrevious->_next = from;
        }
        if (toNext != nullptr) {
            toNext->_previous = from;
        }

        // Update 'from' 's next and previous to point to what 'to' was pointing to.
        from->_next = toNext;
        from->_previous = toPrevious;

        // Update _head and _tail if needed.
        if (fromWasHead == true) {
            _head = to;
        }
        if (toWasHead == true) {
            _head = from;
        }
        if (fromWasTail == true) {
            _tail = to;
        }
        if (toWasTail == true) {
            _tail = from;
        }

        return true;
    }

    void sort(const Comparator<T> &comp) {
        (void) comp;
        // TODO: implement me.
    }

    LinkedListIterator<T> begin() {
        return LinkedListIterator<T>(*this, _head);
    }

    ConstLinkedListIterator<T> begin() const {
        return ConstLinkedListIterator<T>(_head);
    }

    LinkedListIterator<T> end() {
        return LinkedListIterator<T>(*this, _tail);
    }

    ConstLinkedListIterator<T> end() const {
        return ConstLinkedListIterator<T>(_tail);
    }

private:

    LinkedListEntry<T> *findEntry(const std::size_t pos) const {
        LinkedListEntry<T> *entry = _head;
        for (std::size_t i = 0; i < pos; ++i) {
            entry = entry->_next;
            if (entry == nullptr) {
                break;
            }
        }
        return entry;
    }

    Allocator &_allocator;
    LinkedListEntry<T> *_head;
    LinkedListEntry<T> *_tail;
    std::size_t _size;

    // LinkedListIterator<T> needs access to _size, _head, _tail and _allocator.
    friend class LinkedListIterator<T>;
};

}

#endif // LINKEDLIST_H


text:
SET(CMAKE_RELATIVE_PATH_TOP_SOURCE "/Users/deets/projects/private/raspberry-racer/cpp/osx-gmock-1.6.0")
SET(CMAKE_RELATIVE_PATH_TOP_BINARY "/Users/deets/projects/private/raspberry-racer/cpp/osx-gmock-1.6.0")

# Force unix paths in dependencies.
SET(CMAKE_FORCE_UNIX_PATHS 1)

# The C and CXX include file regular expressions for this directory.
SET(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$")
SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$")
SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})
SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN})


text:
enum LogTag{
	LOG_INFO,
	LOG_EVENT,
	LOG_ERROR
};
//  Takes a message and displays it in the system log.
void Log(LogTag tag, const char* title, const char* message);


text:
import BaseMsg = require('./BaseMsg')
import crypto = require('crypto')
import Msg = require('./Msg')
import Q = require('q')
import Response = require('./Response')

class ObjectMsg extends BaseMsg {
    private _json: string
    constructor(statusCode: number, headers: any, private _obj: any) {
        super(statusCode, headers)
    }
    json(): string {
        if (!this._json)
            this._json = JSON.stringify(this._obj)
        return this._json
    }
    sha1(): string {
        var shasum = crypto.createHash('sha1')
        shasum.update(this.json())
        return shasum.digest('hex')
    }
    cache(maxAge: number, etag: string): Msg {
        return super.cache(maxAge, maxAge && !etag ? this.sha1() : etag)
    }
    respond(res: Response): void {
        var body = this._obj && new Buffer(this.json())
        res.setHeader('Content-Length', body ? body.length.toString() : '0')
        if (this.statusCode !== 204)
            res.setHeader('Content-Type', 'application/json')

        this.setHeaders(res)
        if (this.statusCode)
            res.writeHead(this.statusCode)
        res.end(body)
    }
    getBuffer(): Q.Promise<NodeBuffer> {
        return Q.fcall(() => new Buffer(this.json()))
    }
    getString(): Q.Promise<string> {
        return Q.fcall(() => this.json())
    }
    getObject(): Q.Promise<any> {
        return Q.fcall(() => this._obj)
    }
}

export = ObjectMsg


text:
Bug #62081: IntlDateFormatter leaks memory if called twice
--INI--
date.timezone=Atlantic/Azores
--SKIPIF--
<?php
if (!extension_loaded('intl'))
	die('skip intl extension not enabled');
--FILE--
<?php
ini_set('intl.error_level', E_WARNING);
$x = new IntlDateFormatter('en', 1, 1);
var_dump($x->__construct('en', 1, 1));
--EXPECTF--
Warning: IntlDateFormatter::__construct(): datefmt_create: cannot call constructor twice in %s on line %d
NULL


text:
<?php declare(strict_types = 1);

namespace Cmnty\Push\Crypto;

use InvalidArgumentException;

class SharedSecret implements KeyingMaterial
{
    /**
     * @var BinaryString
     */
    private $sharedSecret;

    /**
     * Constructor.
     *
     * @param BinaryString $sharedSecret
     *
     * @throws InvalidArgumentException When the shared secret is not the correct length.
     */
    public function __construct(BinaryString $sharedSecret)
    {
        if ($sharedSecret->getLength() != 32) {
            throw new InvalidArgumentException('SharedSecret could not be created: incorrect length.');
        }

        $this->sharedSecret = $sharedSecret;
    }

    /**
     * Get raw bytes.
     *
     * @return string
     */
    public function getRawKeyMaterial() : string
    {
        return $this->sharedSecret->getRawBytes();
    }
}


text:
@import UIKit;

@interface UIScreen (TWScreenSize)

+ (CGFloat)tw_width;
+ (CGFloat)tw_height;

@end


text:
function getDependentDropdownValues(request, paramKey, paramField, targetField, responseName, keyName, descName, selected, callback, allowEmpty, hide, hideTitle, inputField){
// To dynamically populate a dependent drop-down on change on its parent drop-down, doesn't require any fixed naming convention
// request      = request calling the service which retrieve the info from the DB, ex: getAssociatedStateList
// paramKey     = parameter value used in the called service
// paramField   = parent drop-down field Id (mainId)
// targetField  = dependent drop-down field Id (dependentId)
// responseName = result returned by the service (using a standard json response, ie chaining json request)
// keyName      = keyName of the dependent drop-down
// descName     = name of the dependent drop-down description
// selected     = optional name of a selected option
// callback     = optional javascript function called at end
// allowEmpty   = optional boolean argument, allow selection of an empty value for the dependentId
// hide         = optional boolean argument, if true the dependent drop-down field (targetField) will be hidden when no options are available else only disabled. False by default.
// hideTitle    = optional boolean argument (hide must be set to true), if true the title of the dependent drop-down field (targetField) will be hidden when no options are available else only disabled. False by default.
// inputField   = optional name of an input field
//             this is to handle a specific case where an input field is needed instead of a drop-down when no values are returned by the request
//             this will be maybe extended later to use an auto-completed drop-down or a lookup, instead of straight drop-down currently, when there are too much values to populate
//             this is e.g. currently used in the Product Price Rules screen
    target = '#' + targetField;
    input = '#' + inputField;
    targetTitle = target + '_title'
    optionList = '';
    jQuery.ajax({
        url: request,
        data: [{
            name: paramKey,
            value: jQuery('#' + paramField).val()
        }], // get requested value from parent drop-down field
        async: false,
        type: 'POST',
        success: function(result){
            list = result[responseName];
            // Create and show dependent select options
            if (list) {
                // Scipio: supports new _previous_ selected option; it allows using markup to specify the initial value
                if (selected === '_previous_') {
                    selected = jQuery(target).val();
                    if (!selected) {
                        selected = '';
                    }
                }
                jQuery.each(list, function(key, value){
                    if (typeof value == 'string') {
                        values = value.split(': ');
                        // SCIPIO: TODO: REVIEW: should this be equality check? (selected == values[1]) see OFBIZ-7275 / r1802692 / https://svn.apache.org/repos/asf/ofbiz/branches/release16.11
                        if (selected && values[1].indexOf(selected) >= 0) { // Scipio: check fixed
                            optionList += '<option selected="selected" value="' + values[1] + '">' + values[0] + '</option>';
                        } else {
                            optionList += '<option value="' + values[1] + '">' + values[0] + '</option>';
                        }
                    } else {
                        if (value[keyName] == selected) {
                            optionList += '<option selected="selected" value="' + value[keyName] + '">' + value[descName] + '</option>';
                        } else {
                            optionList += '<option value="' + value[keyName] + '">' + value[descName] + '</option>';
                        }
                    }
                })
                if(allowEmpty) {
                    var preOptionList = "";
                    // Allow null selection in dependent and set it as default if no selection exists.
                    if (!selected || selected === "_none_") { // Scipio: check fixed
                        preOptionList += '<option selected="selected" value=""></option>';
                    } else {
                        preOptionList += "<option value=''></option>";
                    }
                    optionList = preOptionList + optionList;
                }
            };
            // Hide/show the dependent drop-down if hide=true else simply disable/enable
            if ((!list) || (list.length < 1) || ((list.length == 1) && jQuery.inArray("_NA_", list) != -1)) {
                jQuery(target).attr('disabled', 'disabled');
                if (hide) {
                    if (jQuery(target).is(':visible')) {
                        jQuery(target).fadeOut(2500);
                        if (hideTitle) jQuery(targetTitle).fadeOut(2500);
                    } else {
                        jQuery(target).fadeIn();
                        if (hideTitle) jQuery(targetTitle).fadeIn();
                        jQuery(target).fadeOut(2500);
                        if (hideTitle) jQuery(targetTitle).fadeOut(2500);
                    }
                }
            } else {
                jQuery(target).removeAttr('disabled');
                if (hide) {
                    if (!jQuery(target).is(':visible')) {
                        jQuery(target).fadeIn();
                        if (hideTitle) jQuery(targetTitle).fadeIn();
                    }
                }
            }
        },
        complete: function(){
            // this is to handle a specific case where an input field is needed instead of a drop-down when no values are returned by the request (else if allow-empty="true" is used autoComplete handle the case)
            // this could be extended later to use an auto-completed drop-down or a lookup, instead of drop-down currently, when there are too much values to populate
            // Another option is to use an input field with Id instead of a drop-down, see setPriceRulesCondEventJs.ftl and top of getAssociatedPriceRulesConds service
            if (!list && inputField) {
                jQuery(target).hide();
                jQuery(input).show();
            } else if (inputField) {
                jQuery(input).hide();
                jQuery(target).show();
            }
            jQuery(target).html(optionList).click().change(); // .change() needed when using also asmselect on same field, .click() specifically for IE8
            if (callback != null) eval(callback);
        }
    });
}

//*** calls any service already mounted as an event
// arguments must be either a request only (1st argument) or a request followed by {name;value} pair/s parameters
function getServiceResult(){
    var request = arguments[0];
    var params =  new Array();
    var data;
    jQuery.ajax({
        type: 'POST',
        url: request,
        data: prepareAjaxData(arguments),
        async: false,
        cache: false,
        success: function(result){
            data = result;
        }
    });
    return data;
}

function prepareAjaxData(params) {
  var data = new Array();
  if (params.length > 1) {
    for (var i = 1; i < params.length; i++) {
      data.push({
        name: params[i],
        value: params[i + 1]
    });
      i++;
    }
  }
    return data;
}

//*** checkUomConversion returns true if an UomConversion exists
function checkUomConversion(request, params){
    data = getServiceResult(request, params);
    return data['exist'];
}

/* initTimeZone is used to intialise the path to timezones files

The timezone region that loads on initialization is North America (the Olson 'northamerica' file).
To change that to another reqion, set timezoneJS.timezone.defaultZoneFile to your desired region, like so:
  timezoneJS.timezone.zoneFileBasePath = '/tz';
  timezoneJS.timezone.defaultZoneFile = 'asia';
  timezoneJS.timezone.init();

If you want to preload multiple regions, set it to an array, like this:

  timezoneJS.timezone.zoneFileBasePath = '/tz';
  timezoneJS.timezone.defaultZoneFile = ['asia', 'backward', 'northamerica', 'southamerica'];
  timezoneJS.timezone.init();

By default the timezoneJS.Date timezone code lazy-loads the timezone data files, pulling them down and parsing them only as needed.

For example, if you go with the out-of-the-box setup, you'll have all the North American timezones pre-loaded --
but if you were to add a date with a timezone of 'Asia/Seoul,' it would grab the 'asia' Olson file and parse it
before calculating the timezone offset for that date.

You can change this behavior by changing the value of timezoneJS.timezone.loadingScheme. The three possible values are:

  timezoneJS.timezone.loadingSchemes.PRELOAD_ALL -- this will preload all the timezone data files for all reqions up front. This setting would only make sense if you know your users will be using timezones from all around the world, and you prefer taking the up-front load time to the small on-the-fly lag from lazy loading.
  timezoneJS.timezone.loadingSchemes.LAZY_LOAD -- the default. Loads some amount of data up front, then lazy-loads any other needed timezone data as needed.
  timezoneJS.timezone.loadingSchemes.MANUAL_LOAD -- Preloads no data, and does no lazy loading. Use this setting if you're loading pre-parsed JSON timezone data.

  More at https://github.com/mde/timezone-js

*/
function initTimeZone() {
  timezoneJS.timezone.zoneFileBasePath = '/images/date/timezones/min';
  timezoneJS.timezone.loadingSchemes.PRELOAD_ALL;
  timezoneJS.timezone.init();
}


text:
<?php

namespace GearmanManager\Bridge;
use \GearmanManager\GearmanManager;

if (!class_exists("GearmanManager")) {
    require __DIR__."/../GearmanManager.php";
}

/**
 * Implements the worker portions of the PEAR Net_Gearman library
 */
class GearmanPearManager extends GearmanManager {

    public static $LOG = array();

    private $start_time;

    private $last_idle_info_time;
    private $last_idle_debug_time;

    private $worker;

    /**
     * Starts a worker for the PEAR library
     *
     * @param   array   $worker_list    List of worker functions to add
     * @param   array   $timeouts       list of worker timeouts to pass to server
     * @return  void
     *
     */
    protected function start_lib_worker($worker_list, $timeouts = array()) {

        /**
         * Require PEAR Net_Gearman libs
         */
        define('NET_GEARMAN_JOB_PATH', $this->worker_dir);

        if (!class_exists("Net_Gearman_Job_Common")) {
            require "Net/Gearman/Job/Common.php";
        }

        if (!class_exists("Net_Gearman_Job")) {
            require "Net/Gearman/Job.php";
        }

        if (!class_exists("Net_Gearman_Worker")) {
            require "Net/Gearman/Worker.php";
        }

        try {

            $this->worker = new \Net_Gearman_Worker($this->servers);

        } catch (Net_Gearman_Exception $e) {

            $this->log($e->getMessage(), GearmanManager::LOG_LEVEL_WORKER_INFO);
            return;

        }

        $server_status = $this->worker->connection_status();

        if ($server_status["connected"] == 0) {
            $this->log("Failed to connect to any servers", GearmanManager::LOG_LEVEL_WORKER_INFO);
        } elseif ($server_status["disconnected"] > 0) {
            $message = "Failed to connect to the following servers: ";
            foreach ($server_status["servers"] as $server => $status) {
                if (!$status) {
                    $message.= "$server,";
                }
            }
            $message = substr($message, 0, -1);
            $this->log($message, GearmanManager::LOG_LEVEL_WORKER_INFO);
        }

        foreach ($worker_list as $w) {
            $timeout = (isset($timeouts[$w]) ? $timeouts[$w] : null);
            $message = "Adding job $w";
            if ($timeout) {
                $message.= "; timeout: $timeout";
            }
            $this->log($message, GearmanManager::LOG_LEVEL_WORKER_INFO);
            $this->worker->addAbility($w, $timeout, $this->functions[$w]);
        }

        $this->worker->attachCallback(array($this, 'job_start'), \Net_Gearman_Worker::JOB_START);
        $this->worker->attachCallback(array($this, 'job_complete'), \Net_Gearman_Worker::JOB_COMPLETE);
        $this->worker->attachCallback(array($this, 'job_fail'), \Net_Gearman_Worker::JOB_FAIL);

        $this->start_time = time();

        $this->worker->beginWork(array($this, "monitor"));

    }

    /**
     * Monitor call back for worker. Return true to stop worker
     *
     * @param   bool    $idle       If true the worker was idle
     * @param   int     $lastJob    The time the last job was run
     * @return  bool
     *
     */
    public function monitor($idle, $lastJob) {

        if ($this->max_run_time > 0 && time() - $this->start_time > $this->max_run_time) {
            $this->log("Been running too long, exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
            $this->stop_work = true;
        }

        if (!empty($this->config["max_runs_per_worker"]) && $this->job_execution_count >= $this->config["max_runs_per_worker"]) {
            $this->log("Ran $this->job_execution_count jobs which is over the maximum({$this->config['max_runs_per_worker']}), exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
            $this->stop_work = true;
        }

        if (!$this->stop_work) {

            $time = time() - $lastJob;

            if (empty($this->last_idle_info_time)) {
                $this->last_idle_info_time = time();
            }

            if (empty($this->last_idle_debug_time)) {
                $this->last_idle_debug_time = time();
            }

            $servers = $this->worker->connection_status();

            $connected_servers = array();
            $disconnected_servers = array();

            foreach ($servers["servers"] as $server => $connected) {
                if ($connected) {
                    $connected_servers[] = $server;
                } else {
                    $disconnected_servers[] = $server;
                }
            }

            /**
             * If we are disconnected to any servers, log as info the idle status
             * every 30 seconds.
             *
             * Otherwise, log it at an interval based on max run time if set.
             */
            if ((count($disconnected_servers) > 0 && time() - $this->last_idle_info_time >= 30) ||
               ($this->max_run_time > 0 && time() - $this->last_idle_info_time >= $this->max_run_time/50)) {

                $level = GearmanManager::LOG_LEVEL_WORKER_INFO;
                $this->last_idle_info_time = time();

            } elseif (time() - $this->last_idle_debug_time >= 10) {

                $level = GearmanManager::LOG_LEVEL_DEBUG;
                $this->last_idle_debug_time = time();

            } else {

                $level = GearmanManager::LOG_LEVEL_CRAZY;
            }

            $idle_message = "Worker as been idle for $time seconds.";

            if (count($connected_servers)) {
                $idle_message.=" Connected to ".implode(",", $connected_servers).".";
            }

            if (count($disconnected_servers)) {
                $idle_message.=" Disconnected from ".implode(",", $disconnected_servers).".";
            }

            $this->log($idle_message, $level);

        }

        return $this->stop_work;
    }

    /**
     * Call back for when jobs are started
     */
    public function job_start($handle, $job, $args) {
        $this->job_execution_count++;
        if ( ! empty($this->config["max_runs_per_worker"]) ) {
            $message = sprintf('(%s) Starting Job (%d/%d): %s', $handle, $this->job_execution_count, $this->config["max_runs_per_worker"], $job);
            $this->log($message, GearmanManager::LOG_LEVEL_WORKER_INFO);
        } else {
            $this->log("($handle) Starting Job: $job", GearmanManager::LOG_LEVEL_WORKER_INFO);
        }
        $this->log("($handle) Workload: ".json_encode($args), GearmanManager::LOG_LEVEL_DEBUG);
        self::$LOG = array();
    }

    /**
     * Call back for when jobs are completed
     */
    public function job_complete($handle, $job, $result) {

        $this->log("($handle) Completed Job: $job", GearmanManager::LOG_LEVEL_WORKER_INFO);

        $this->log_result($handle, $result);
    }

    /**
     * Call back for when jobs fail
     */
    public function job_fail($handle, $job, $result) {

        $message = "($handle) Failed Job: $job: ".$result->getMessage();

        $this->log($message, GearmanManager::LOG_LEVEL_WORKER_INFO);

        $this->log_result($handle, $result);
    }

    /**
     * Logs the result of complete/failed jobs
     *
     * @param   mixed   $result     Result returned from worker
     * @return  void
     *
     */
    private function log_result($handle, $result) {

        if (!empty(self::$LOG)) {
            foreach (self::$LOG as $l) {

                if (!is_scalar($l)) {
                    $l = explode("\n", trim(print_r($l, true)));
                } elseif (strlen($l) > 256) {
                    $l = substr($l, 0, 256)."...(truncated)";
                }

                if (is_array($l)) {
                    $log_message = "";
                    foreach ($l as $ln) {
                        $log_message.= "($handle) $ln\n";
                    }
                    $this->log($log_message, GearmanManager::LOG_LEVEL_WORKER_INFO);
                } else {
                    $this->log("($handle) $l", GearmanManager::LOG_LEVEL_WORKER_INFO);
                }

            }
        }

        $result_log = $result;

        if (!is_scalar($result_log)) {
            $result_log = explode("\n", trim(print_r($result_log, true)));
        } elseif (strlen($result_log) > 256) {
            $result_log = substr($result_log, 0, 256)."...(truncated)";
        }

        if (is_array($result_log)) {
            $log_message = "";
            foreach ($result_log as $ln) {
                $log_message.="($handle) $ln\n";
            }
            $this->log($log_message, GearmanManager::LOG_LEVEL_DEBUG);
        } else {
            $this->log("($handle) $result_log", GearmanManager::LOG_LEVEL_DEBUG);
        }
    }

    /**
     * Validates the PECL compatible worker files/functions
     */
    protected function validate_lib_workers() {

        /**
         * Yes, we include these twice because this function is called
         * by a different process than the other location where these
         * are included.
         */
        if (!class_exists("Net_Gearman_Job_Common")) {
            require "Net/Gearman/Job/Common.php";
        }

        if (!class_exists("Net_Gearman_Job")) {
            require "Net/Gearman/Job.php";
        }

        /**
         * Validate functions
         */
        foreach ($this->functions as $name => $func) {
            $class = $this->prefix.$name;
            if (!class_exists($class, false)) {
                include $func['path'];
            }
            if (!class_exists($class, false) && !method_exists($class, "run")) {
                $this->log("Class $class not found in {$func['path']} or run method not present");
                posix_kill($this->pid, SIGUSR2);
                exit();
            }
        }

    }

}