<commit_before>"""The top level of the ASN.1 tools package contains commonly used
functions and classes, and the command line interface.

"""

import sys
import argparse
import binascii
import logging

from .compiler import compile_dict
from .compiler import compile_string
from .compiler import compile_files
from .compiler import pre_process_dict
from .parser import parse_string
from .parser import parse_files
from .parser import ParseError
from .errors import EncodeError
from .errors import DecodeError
from .errors import CompileError

__author__ = 'Erik Moqvist'
__version__ = '0.48.0'

def _decode_hexstring(codec_spec, asn1_spec, type_name, hexstring):
    try:
        encoded = binascii.unhexlify(hexstring)
    except Exception as e:
        raise TypeError("'{}': {}".format(hexstring, str(e)))

    decoded = codec_spec.decode(type_name, encoded)

    print(asn1_spec.encode(type_name, decoded, indent=4).decode('utf-8'))

def _do_decode(args):
    parsed = parse_files(args.specification)
    codec_spec = compile_dict(parsed, args.codec)
    asn1_spec = compile_dict(parsed, 'asn1')

    if args.hexstring == '-':
        for hexstring in sys.stdin:
            hexstring = hexstring.strip('\r\n')

            if hexstring:
                try:
                    _decode_hexstring(codec_spec,
                                      asn1_spec,
                                      args.type,
                                      hexstring)
                except TypeError:
                    print(hexstring)
                except DecodeError as e:
                    print(hexstring)
                    print(str(e))
            else:
                print(hexstring)
    else:
        _decode_hexstring(codec_spec,
                          asn1_spec,
                          args.type,
                          args.hexstring)

def _main():
    parser = argparse.ArgumentParser(
        description='Various ASN.1 utilities.')

    parser.add_argument('-d', '--debug', action='store_true')
    parser.add_argument('-v', '--verbose',
                        choices=(0, 1, 2),
                        type=int,
                        default=1,
                        help=('Control the verbosity; disable(0), warning(1) '
                              'and debug(2). (default: 1).'))
    parser.add_argument('--version',
                        action='version',
                        version=__version__,
                        help='Print version information and exit.')

    # Workaround to make the subparser required in Python 3.
    subparsers = parser.add_subparsers(title='subcommands',
                                       dest='subcommand')
    subparsers.required = True

    # The 'decode' subparser.
    decode_parser = subparsers.add_parser(
        'decode',
        description='Decode given hextring and print it to standard output.')
    decode_parser.add_argument('-c', '--codec',
                               choices=('ber', 'der', 'jer', 'per', 'uper', 'xer'),
                               default='ber',
                               help='Codec (default: ber).')
    decode_parser.add_argument('specification',
                               nargs='+',
                               help='ASN.1 specification as one or more .asn files.')
    decode_parser.add_argument('type', help='Type to decode.')
    decode_parser.add_argument(
        'hexstring',
        help='Hexstring to decode, or - to read hexstrings from standard input.')
    decode_parser.set_defaults(func=_do_decode)

    args = parser.parse_args()

    levels = [logging.CRITICAL, logging.WARNING, logging.DEBUG]
    level = levels[args.verbose]

    logging.basicConfig(format='%(levelname)s: %(message)s',
                        level=level)

    if args.debug:
        args.func(args)
    else:
        try:
            args.func(args)
        except BaseException as e:
            sys.exit('error: {}'.format(str(e)))
<commit_msg>Version 0.49.0.<commit_after>"""The top level of the ASN.1 tools package contains commonly used
functions and classes, and the command line interface.

"""

import sys
import argparse
import binascii
import logging

from .compiler import compile_dict
from .compiler import compile_string
from .compiler import compile_files
from .compiler import pre_process_dict
from .parser import parse_string
from .parser import parse_files
from .parser import ParseError
from .errors import EncodeError
from .errors import DecodeError
from .errors import CompileError

__author__ = 'Erik Moqvist'
__version__ = '0.49.0'

def _decode_hexstring(codec_spec, asn1_spec, type_name, hexstring):
    try:
        encoded = binascii.unhexlify(hexstring)
    except Exception as e:
        raise TypeError("'{}': {}".format(hexstring, str(e)))

    decoded = codec_spec.decode(type_name, encoded)

    print(asn1_spec.encode(type_name, decoded, indent=4).decode('utf-8'))

def _do_decode(args):
    parsed = parse_files(args.specification)
    codec_spec = compile_dict(parsed, args.codec)
    asn1_spec = compile_dict(parsed, 'asn1')

    if args.hexstring == '-':
        for hexstring in sys.stdin:
            hexstring = hexstring.strip('\r\n')

            if hexstring:
                try:
                    _decode_hexstring(codec_spec,
                                      asn1_spec,
                                      args.type,
                                      hexstring)
                except TypeError:
                    print(hexstring)
                except DecodeError as e:
                    print(hexstring)
                    print(str(e))
            else:
                print(hexstring)
    else:
        _decode_hexstring(codec_spec,
                          asn1_spec,
                          args.type,
                          args.hexstring)

def _main():
    parser = argparse.ArgumentParser(
        description='Various ASN.1 utilities.')

    parser.add_argument('-d', '--debug', action='store_true')
    parser.add_argument('-v', '--verbose',
                        choices=(0, 1, 2),
                        type=int,
                        default=1,
                        help=('Control the verbosity; disable(0), warning(1) '
                              'and debug(2). (default: 1).'))
    parser.add_argument('--version',
                        action='version',
                        version=__version__,
                        help='Print version information and exit.')

    # Workaround to make the subparser required in Python 3.
    subparsers = parser.add_subparsers(title='subcommands',
                                       dest='subcommand')
    subparsers.required = True

    # The 'decode' subparser.
    decode_parser = subparsers.add_parser(
        'decode',
        description='Decode given hextring and print it to standard output.')
    decode_parser.add_argument('-c', '--codec',
                               choices=('ber', 'der', 'jer', 'per', 'uper', 'xer'),
                               default='ber',
                               help='Codec (default: ber).')
    decode_parser.add_argument('specification',
                               nargs='+',
                               help='ASN.1 specification as one or more .asn files.')
    decode_parser.add_argument('type', help='Type to decode.')
    decode_parser.add_argument(
        'hexstring',
        help='Hexstring to decode, or - to read hexstrings from standard input.')
    decode_parser.set_defaults(func=_do_decode)

    args = parser.parse_args()

    levels = [logging.CRITICAL, logging.WARNING, logging.DEBUG]
    level = levels[args.verbose]

    logging.basicConfig(format='%(levelname)s: %(message)s',
                        level=level)

    if args.debug:
        args.func(args)
    else:
        try:
            args.func(args)
        except BaseException as e:
            sys.exit('error: {}'.format(str(e)))
<|endoftext|>


<commit_before>def level_one_user(self):
<commit_msg>Delete proxy.py<commit_after><|endoftext|>


<commit_before># Copyright (c) 2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of Google Inc. nor the names of its contributors
#    may be used to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
r"""Training binary for all neural network models using a custom training loop.

To use this binary for training a specific model, the corresponding config file
should be specified as input. Example usage:

CONFIG="//learning/genomics/deepconsensus/models/model_configs.py:transformer_learn_values+ccs"
OUT_DIR=/tmp

time blaze run -c opt \
//learning/genomics/deepconsensus/models:model_train_custom_loop -- \
  --params ${CONFIG} \
  --out_dir ${OUT_DIR} \
  --alsologtostderr
"""

import logging
import os
import random
from typing import Optional

from absl import app
from absl import flags
import ml_collections
from ml_collections.config_flags import config_flags
import tensorflow as tf

from deepconsensus.models import model_utils

# pylint: disable=unused-import

FLAGS = flags.FLAGS
config_flags.DEFINE_config_file('params', None, 'Training configuration.')
flags.DEFINE_string('out_dir', None,
                    'Output path for logs and model checkpoints.')
flags.DEFINE_string(
    'tpu', None, 'Name of the TPU to use. This gets '
    'populated automatically when using XManager.')
flags.DEFINE_string('tpu_topology', None, 'Tpu topology.')
flags.DEFINE_bool('debug', False,
                  'Enables dumping debug info for TensorBoard Debugger V2.')
flags.DEFINE_bool(
    'write_checkpoint_metrics', False,
    'Whether to write eval metrics for each checkpoint during training.')
flags.DEFINE_bool(
    'eval_and_log_every_step', False, 'Eval and log after every step. '
    'Use this e.g. for testing training and inspecting metrics locally.')

def train_model(out_dir: str, params: ml_collections.ConfigDict,
                strategy: tf.distribute.Strategy,
                write_checkpoint_metrics: bool) -> None:
  """Trains the model under the given strategy and params."""
  # Freeze config dict here to ensure it is hashable.
  params = ml_collections.FrozenConfigDict(params)
  model_utils.save_params_as_json(out_dir, params)
  train_dataset, eval_dataset = model_utils.get_datasets(params, strategy)
  steps_per_epoch, steps_per_eval = model_utils.get_step_counts(
      params, FLAGS.eval_and_log_every_step)

  with strategy.scope():
    logging.info('Building model.')
    model = model_utils.get_model(params)
    logging.info('Done building model.')
    optimizer = tf.keras.optimizers.Adam(learning_rate=params.learning_rate)
    train_loss = tf.keras.metrics.Mean(name=params.loss_function)
    train_metrics = model_utils.get_deepconsensus_metrics(name_prefix='')
    eval_loss = tf.keras.metrics.Mean(name=params.loss_function)
    eval_metrics = model_utils.get_deepconsensus_metrics(name_prefix='')
    loss_object = model_utils.get_deepconsensus_loss(
        params, reduction=tf.keras.losses.Reduction.NONE)

    def compute_loss(labels, predictions):
      per_example_loss = loss_object(labels, predictions)
      # We divide per-replica losses by global batch size and sum this value
      # across all replicas to compute average loss scaled by global batch size.
      return tf.nn.compute_average_loss(
          per_example_loss, global_batch_size=params.batch_size)

    # model, optimizer, and checkpoint must be created under `strategy.scope`.
    checkpoint, initial_epoch = model_utils.get_checkpoint_and_initial_epoch(
        model, optimizer, out_dir, steps_per_epoch)  # pytype: disable=wrong-arg-types  # typed-keras

  # Create summary writers
  train_writer = tf.summary.create_file_writer(os.path.join(out_dir, 'train'))
  eval_writer = tf.summary.create_file_writer(os.path.join(out_dir, 'eval'))

  def train_step(inputs):
    """Training StepFn."""
    features, labels = inputs
    with tf.GradientTape() as tape:
      predictions = model(features)
      loss = compute_loss(labels, predictions)
    grads = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))
    train_loss.update_state(loss)
    for metric in train_metrics:
      metric.update_state(labels, predictions)
    return loss

  def eval_step(inputs):
    """Eval StepFn."""
    features, labels = inputs
    predictions = model(features)
    loss = compute_loss(labels, predictions)
    eval_loss.update_state(loss)
    for metric in eval_metrics:
      metric.update_state(labels, predictions)
    return loss

  @tf.function
  def distributed_train_step(iterator):
    per_replica_losses = strategy.run(train_step, args=(next(iterator),))
    return strategy.reduce(
        tf.distribute.ReduceOp.SUM, per_replica_losses, axis=None)

  @tf.function
  def distributed_eval_step(iterator):
    per_replica_losses = strategy.run(eval_step, args=(next(iterator),))
    return strategy.reduce(
        tf.distribute.ReduceOp.SUM, per_replica_losses, axis=None)

  log_steps = 100
  if FLAGS.eval_and_log_every_step:
    log_steps = 1
  train_iterator = iter(train_dataset)
  eval_iterator = iter(eval_dataset)
  min_eval_loss = 1e6
  for epoch in range(initial_epoch, params['num_epochs']):
    logging.info('Starting to run epoch: %s', epoch)
    with train_writer.as_default():
      for step in range(steps_per_epoch):
        reduced_train_loss = distributed_train_step(train_iterator)
        if step % log_steps == 0:
          train_loss_dict = {train_loss.name: reduced_train_loss}
          model_utils.log_and_save_metrics(
              epoch=epoch,
              step=step,
              total_steps=steps_per_epoch,
              optimizer=optimizer,
              losses_dict=train_loss_dict,
              metrics=train_metrics,
              training=True)
    with eval_writer.as_default():
      for step in range(steps_per_eval):
        reduced_eval_loss = distributed_eval_step(eval_iterator)
      eval_loss_dict = {eval_loss.name: reduced_eval_loss}
      model_utils.log_and_save_metrics(
          epoch=epoch,
          step=step,
          total_steps=steps_per_eval,
          optimizer=optimizer,
          losses_dict=eval_loss_dict,
          metrics=eval_metrics,
          training=False)
    checkpoint_name = model_utils.save_checkpoint(checkpoint, out_dir,
                                                  train_metrics, eval_metrics,
                                                  write_checkpoint_metrics)
    if min_eval_loss > float(eval_loss.result()):
      min_eval_loss = float(eval_loss.result())
      with tf.io.gfile.GFile(os.path.join(out_dir, 'best_checkpoint.txt'),
                             'w') as f:
        f.write(os.path.basename(checkpoint_name))
    model_utils.reset_all_metrics([train_loss, eval_loss] + train_metrics +
                                  eval_metrics)

def train(out_dir: str,
          params: ml_collections.ConfigDict,
          tpu: Optional[str],
          tpu_topology: Optional[str],
          write_checkpoint_metrics: bool,
          debug: Optional[bool] = False):
  """Run the model training and return evaluation output."""
  model_utils.modify_params(params, tpu=tpu, tpu_topology=tpu_topology)
  random.seed(params.seed)
  tf.random.set_seed(params.seed)
  os.environ['TF_ENABLE_EAGER_CLIENT_STREAMING_ENQUEUE'] = 'False'
  while True:
    try:
      if tpu is not None:
        resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu=tpu)
        tf.config.experimental_connect_to_cluster(resolver)
        tf.tpu.experimental.initialize_tpu_system(resolver)
        strategy = tf.distribute.TPUStrategy(resolver)
      elif debug:
        strategy = tf.distribute.OneDeviceStrategy(device='/cpu:0')
      else:
        strategy = tf.distribute.MirroredStrategy()
      train_model(out_dir, params, strategy, write_checkpoint_metrics)
      break
    except tf.errors.UnavailableError:
      continue

def main(unused_args=None):
  train(FLAGS.out_dir, FLAGS.params, FLAGS.tpu, FLAGS.tpu_topology,
        FLAGS.write_checkpoint_metrics, FLAGS.debug)

if __name__ == '__main__':
  flags.mark_flags_as_required([
      'params',
      'out_dir',
  ])
  app.run(main)
<commit_msg>Internal change.<commit_after># Copyright (c) 2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of Google Inc. nor the names of its contributors
#    may be used to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
r"""Training binary for all neural network models using a custom training loop.

To use this binary for training a specific model, the corresponding config file
should be specified as input. Example usage:

CONFIG="//learning/genomics/deepconsensus/models/model_configs.py:transformer_learn_values+ccs"
OUT_DIR=/tmp

time blaze run -c opt \
//learning/genomics/deepconsensus/models:model_train_custom_loop -- \
  --params ${CONFIG} \
  --out_dir ${OUT_DIR} \
  --alsologtostderr
"""

import logging
import os
import random
from typing import Optional

from absl import app
from absl import flags
import ml_collections
from ml_collections.config_flags import config_flags
import tensorflow as tf

from deepconsensus.models import model_utils
# pylint: disable=unused-import g-import-not-at-top

FLAGS = flags.FLAGS
config_flags.DEFINE_config_file('params', None, 'Training configuration.')
flags.DEFINE_string('out_dir', None,
                    'Output path for logs and model checkpoints.')
flags.DEFINE_string(
    'tpu', None, 'Name of the TPU to use. This gets '
    'populated automatically when using XManager.')
flags.DEFINE_string('tpu_topology', None, 'Tpu topology.')
flags.DEFINE_bool('debug', False,
                  'Enables dumping debug info for TensorBoard Debugger V2.')
flags.DEFINE_bool(
    'write_checkpoint_metrics', False,
    'Whether to write eval metrics for each checkpoint during training.')
flags.DEFINE_bool(
    'eval_and_log_every_step', False, 'Eval and log after every step. '
    'Use this e.g. for testing training and inspecting metrics locally.')

def train_model(out_dir: str, params: ml_collections.ConfigDict,
                strategy: tf.distribute.Strategy,
                write_checkpoint_metrics: bool) -> None:
  """Trains the model under the given strategy and params."""
  # Freeze config dict here to ensure it is hashable.
  params = ml_collections.FrozenConfigDict(params)
  model_utils.save_params_as_json(out_dir, params)
  train_dataset, eval_dataset = model_utils.get_datasets(params, strategy)
  steps_per_epoch, steps_per_eval = model_utils.get_step_counts(
      params, FLAGS.eval_and_log_every_step)

  with strategy.scope():
    logging.info('Building model.')
    model = model_utils.get_model(params)
    logging.info('Done building model.')
    optimizer = tf.keras.optimizers.Adam(learning_rate=params.learning_rate)
    train_loss = tf.keras.metrics.Mean(name=params.loss_function)
    train_metrics = model_utils.get_deepconsensus_metrics(name_prefix='')
    eval_loss = tf.keras.metrics.Mean(name=params.loss_function)
    eval_metrics = model_utils.get_deepconsensus_metrics(name_prefix='')
    loss_object = model_utils.get_deepconsensus_loss(
        params, reduction=tf.keras.losses.Reduction.NONE)

    def compute_loss(labels, predictions):
      per_example_loss = loss_object(labels, predictions)
      # We divide per-replica losses by global batch size and sum this value
      # across all replicas to compute average loss scaled by global batch size.
      return tf.nn.compute_average_loss(
          per_example_loss, global_batch_size=params.batch_size)

    # model, optimizer, and checkpoint must be created under `strategy.scope`.
    checkpoint, initial_epoch = model_utils.get_checkpoint_and_initial_epoch(
        model, optimizer, out_dir, steps_per_epoch)  # pytype: disable=wrong-arg-types  # typed-keras

  # Create summary writers
  train_writer = tf.summary.create_file_writer(os.path.join(out_dir, 'train'))
  eval_writer = tf.summary.create_file_writer(os.path.join(out_dir, 'eval'))

  def train_step(inputs):
    """Training StepFn."""
    features, labels = inputs
    with tf.GradientTape() as tape:
      predictions = model(features)
      loss = compute_loss(labels, predictions)
    grads = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))
    train_loss.update_state(loss)
    for metric in train_metrics:
      metric.update_state(labels, predictions)
    return loss

  def eval_step(inputs):
    """Eval StepFn."""
    features, labels = inputs
    predictions = model(features)
    loss = compute_loss(labels, predictions)
    eval_loss.update_state(loss)
    for metric in eval_metrics:
      metric.update_state(labels, predictions)
    return loss

  @tf.function
  def distributed_train_step(iterator):
    per_replica_losses = strategy.run(train_step, args=(next(iterator),))
    return strategy.reduce(
        tf.distribute.ReduceOp.SUM, per_replica_losses, axis=None)

  @tf.function
  def distributed_eval_step(iterator):
    per_replica_losses = strategy.run(eval_step, args=(next(iterator),))
    return strategy.reduce(
        tf.distribute.ReduceOp.SUM, per_replica_losses, axis=None)

  log_steps = 100
  if FLAGS.eval_and_log_every_step:
    log_steps = 1
  train_iterator = iter(train_dataset)
  eval_iterator = iter(eval_dataset)
  min_eval_loss = 1e6
  for epoch in range(initial_epoch, params['num_epochs']):
    logging.info('Starting to run epoch: %s', epoch)
    with train_writer.as_default():
      for step in range(steps_per_epoch):
        reduced_train_loss = distributed_train_step(train_iterator)
        if step % log_steps == 0:
          train_loss_dict = {train_loss.name: reduced_train_loss}
          model_utils.log_and_save_metrics(
              epoch=epoch,
              step=step,
              total_steps=steps_per_epoch,
              optimizer=optimizer,
              losses_dict=train_loss_dict,
              metrics=train_metrics,
              training=True)
    with eval_writer.as_default():
      for step in range(steps_per_eval):
        reduced_eval_loss = distributed_eval_step(eval_iterator)
      eval_loss_dict = {eval_loss.name: reduced_eval_loss}
      model_utils.log_and_save_metrics(
          epoch=epoch,
          step=step,
          total_steps=steps_per_eval,
          optimizer=optimizer,
          losses_dict=eval_loss_dict,
          metrics=eval_metrics,
          training=False)
    checkpoint_name = model_utils.save_checkpoint(checkpoint, out_dir,
                                                  train_metrics, eval_metrics,
                                                  write_checkpoint_metrics)
    if min_eval_loss > float(eval_loss.result()):
      min_eval_loss = float(eval_loss.result())
      with tf.io.gfile.GFile(os.path.join(out_dir, 'best_checkpoint.txt'),
                             'w') as f:
        f.write(os.path.basename(checkpoint_name))
    model_utils.reset_all_metrics([train_loss, eval_loss] + train_metrics +
                                  eval_metrics)

def train(out_dir: str,
          params: ml_collections.ConfigDict,
          tpu: Optional[str],
          tpu_topology: Optional[str],
          write_checkpoint_metrics: bool,
          debug: Optional[bool] = False):
  """Run the model training and return evaluation output."""
  model_utils.modify_params(params, tpu=tpu, tpu_topology=tpu_topology)
  random.seed(params.seed)
  tf.random.set_seed(params.seed)
  os.environ['TF_ENABLE_EAGER_CLIENT_STREAMING_ENQUEUE'] = 'False'
  while True:
    try:
      if tpu is not None:
        resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu=tpu)
        tf.config.experimental_connect_to_cluster(resolver)
        tf.tpu.experimental.initialize_tpu_system(resolver)
        strategy = tf.distribute.TPUStrategy(resolver)
      elif debug:
        strategy = tf.distribute.OneDeviceStrategy(device='/cpu:0')
      else:
        strategy = tf.distribute.MirroredStrategy()
      train_model(out_dir, params, strategy, write_checkpoint_metrics)
      break
    except tf.errors.UnavailableError:
      continue

def main(unused_args=None):
  train(FLAGS.out_dir, FLAGS.params, FLAGS.tpu, FLAGS.tpu_topology,
        FLAGS.write_checkpoint_metrics, FLAGS.debug)

if __name__ == '__main__':
  flags.mark_flags_as_required([
      'params',
      'out_dir',
  ])
  app.run(main)
<|endoftext|>


<commit_before>'''
Copyright (C) 2012-2013  Diego Torres Milano
Created on Dec 1, 2012

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@author: Diego Torres Milano
'''

__version__ = '4.3.5'

import sys
import socket
import time
import re
import signal
import os
import types
import platform

DEBUG = False

HOSTNAME = 'localhost'
PORT = 5037

OKAY = 'OKAY'
FAIL = 'FAIL'

UP = 0
DOWN = 1
DOWN_AND_UP = 2

TIMEOUT = 15

class Device:
    @staticmethod
    def factory(_str):
        if DEBUG:
            print >> sys.stderr, "Device.factory(", _str, ")"
        values = _str.split(None, 2)
        if DEBUG:
            print >> sys.stderr, "values=", values
        return Device(*values)

    def __init__(self, serialno, status, qualifiers=None):
        self.serialno = serialno
        self.status = status
        self.qualifiers = qualifiers

    def __str__(self):
        return "<<<" + self.serialno + ", " + self.status + ", %s>>>" % self.qualifiers

class AdbClient:

    def __init__(self, serialno, hostname=HOSTNAME, port=PORT, settransport=True, reconnect=True):
        if not serialno:
            raise ValueError("serialno must not be empty or None")
        self.serialno = serialno
        self.hostname = hostname
        self.port = port

        self.reconnect = reconnect
        self.__connect()

        self.checkVersion()
        self.isTransportSet = False
        if settransport:
            self.__setTransport()

    @staticmethod
    def setAlarm(timeout):
        osName = platform.system()
        if osName.startswith('Windows'): # alarm is not implemented in Windows
            return
        if DEBUG:
            print >> sys.stderr, "setAlarm(%d)" % timeout
        signal.alarm(timeout)

    def setReconnect(self, val):
        self.reconnect = val

    def __connect(self):
        if DEBUG:
            print >> sys.stderr, "__connect()"
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.settimeout(TIMEOUT)
        try:
            self.socket.connect((self.hostname, self.port))
        except socket.error, ex:
            raise RuntimeError("ERROR: Connecting to %s:%d: %s" % (self.socket, self.port, ex))

    def close(self):
        if DEBUG:
            print >> sys.stderr, "Closing socket...", self.socket
        if self.socket:
            self.socket.close()

    def __del__(self):
        try:
            self.close()
        except:
            pass

    def __send(self, msg, checkok=True, reconnect=False):
        if DEBUG:
            print >> sys.stderr, "__send(%s, checkok=%s, reconnect=%s)" % (msg, checkok, reconnect)
        if not re.search('^host:', msg):
            if not self.isTransportSet:
                self.__setTransport()
        else:
            self.checkConnected()
        self.socket.send('%04X%s' % (len(msg), msg))
        if checkok:
            self.__checkOk()
        if reconnect:
            if DEBUG:
                print >> sys.stderr, "    __send: reconnecting"
            self.__connect()
            self.__setTransport()

    def __receive(self, nob=None):
        if DEBUG:
            print >> sys.stderr, "__receive()"
        self.checkConnected()
        if nob is None:
            nob = int(self.socket.recv(4), 16)
        if DEBUG:
            print >> sys.stderr, "    __receive: receiving", nob, "bytes"
        recv = bytearray()
        nr = 0
        while nr < nob:
            chunk = self.socket.recv(min((nob-nr), 4096))
            recv.extend(chunk)
            nr += len(chunk)
        if DEBUG:
            print >> sys.stderr, "    __receive: returning len=", len(recv)
        return str(recv)

    def __checkOk(self):
        if DEBUG:
            print >> sys.stderr, "__checkOk()"
        self.checkConnected()
        self.setAlarm(TIMEOUT)
        recv = self.socket.recv(4)
        if DEBUG:
            print >> sys.stderr, "    __checkOk: recv=", repr(recv)
        if recv != OKAY:
            error = self.socket.recv(1024)
            raise RuntimeError("ERROR: %s %s" % (repr(recv), error))
        self.setAlarm(0)
        if DEBUG:
            print >> sys.stderr, "    __checkOk: returning True"
        return True

    def checkConnected(self):
        if DEBUG:
            print >> sys.stderr, "checkConnected()"
        if not self.socket:
            raise RuntimeError("ERROR: Not connected")
        if DEBUG:
            print >> sys.stderr, "    checkConnected: returning True"
        return True

    def checkVersion(self, reconnect=True):
        if DEBUG:
            print >> sys.stderr, "checkVersion(reconnect=%s)" % reconnect
        self.__send('host:version', reconnect=False)
        version = self.socket.recv(8)
        VERSION='0004001f'
        if version != VERSION:
            raise RuntimeError("ERROR: Incorrect ADB server version %s (expecting %s)" % (version, VERSION))
        if reconnect:
            self.__connect()

    def __setTransport(self):
        if DEBUG:
            print >> sys.stderr, "__setTransport()"
        self.checkConnected()
        serialnoRE = re.compile(self.serialno)
        found = False
        for device in self.getDevices():
            if serialnoRE.match(device.serialno):
                found = True
                break
        if not found:
            raise RuntimeError("ERROR: couldn't find device that matches '%s'" % self.serialno)
        self.serialno = device.serialno
        msg = 'host:transport:%s' % self.serialno
        if DEBUG:
            print >> sys.stderr, "    __setTransport: msg=", msg
        self.__send(msg, reconnect=False)
        self.isTransportSet = True

    def getDevices(self):
        if DEBUG:
            print >> sys.stderr, "getDevices()"
        self.__send('host:devices-l', checkok=False)
        try:
            self.__checkOk()
        except RuntimeError, ex:
            print >> sys.stderr, "**ERROR:", ex
            return None
        devices = []
        for line in self.__receive().splitlines():
            devices.append(Device.factory(line))
        self.__connect()
        return devices

    def shell(self, cmd=None):
        if DEBUG:
            print >> sys.stderr, "shell(cmd=%s)" % cmd
        if cmd:
            self.__send('shell:%s' % cmd, checkok=True, reconnect=False)
            out = ''
            while True:
                _str = None
                try:
                    _str = self.socket.recv(4096)
                except Exception, ex:
                    print >> sys.stderr, "ERROR:", ex
                if not _str:
                    break
                out += _str
            if self.reconnect:
                if DEBUG:
                    print >> sys.stderr, "Reconnecting..."
                self.close()
                self.__connect()
                self.__setTransport()
            return out
        else:
            self.__send('shell:')
            #sin = self.socket.makefile("rw")
            #sout = self.socket.makefile("r")
            #return (sin, sin)
            sout = adbClient.socket.makefile("r")
            return sout

    def __getRestrictedScreen(self):
        ''' Gets C{mRestrictedScreen} values from dumpsys. This is a method to obtain display dimensions '''

        rsRE = re.compile('\s*mRestrictedScreen=\((?P<x>\d+),(?P<y>\d+)\) (?P<w>\d+)x(?P<h>\d+)')
        for line in self.shell('dumpsys window').splitlines():
            m = rsRE.match(line)
            if m:
                return m.groups()
        raise RuntimeError("Couldn't find mRestrictedScreen in dumpsys")

    def __getProp(self, key, strip=True):
        prop = self.shell('getprop %s' % key)
        if strip:
            prop = prop.rstrip('\r\n')
        return prop

    def __getDisplayWidth(self, key, strip=True):
        (x, y, w, h) = self.__getRestrictedScreen()
        return int(w)

    def __getDisplayHeight(self, key, strip=True):
        (x, y, w, h) = self.__getRestrictedScreen()
        return int(h)

    def getSystemProperty(self, key, strip=True):
        return self.getProperty(key, strip)

    def getProperty(self, key, strip=True):
        ''' Gets the property value for key '''

        import collections
        MAP_KEYS = collections.OrderedDict([
                          (re.compile('display.width'), self.__getDisplayWidth),
                          (re.compile('display.height'), self.__getDisplayHeight),
                          (re.compile('.*'), self.__getProp),
                          ])
        '''Maps properties key values (as regexps) to instance methods to obtain its values.'''

        for kre in MAP_KEYS.keys():
            if kre.match(key):
                return MAP_KEYS[kre](key=key, strip=strip)
        raise ValueError("key='%s' does not match any map entry")

    def press(self, name, eventType=DOWN_AND_UP):
        cmd = 'input keyevent %s' % name
        if DEBUG:
            print >>sys.stderr, "press(%s)" % cmd
        self.shell(cmd)

    def startActivity(self, component=None, flags=None, uri=None):
        cmd = 'am start'
        if component:
            cmd += ' -n %s' % component
        if flags:
            cmd += ' -f %s' % flags
        if uri:
            cmd += ' %s' % uri
        self.shell(cmd)

    def takeSnapshot(self, reconnect=False):
        '''
        Takes a snapshot of the device and return it as a PIL Image.
        '''

        try:
            from PIL import Image
        except:
            raise Exception("You have to install PIL to use takeSnapshot()")
        self.__send('framebuffer:', checkok=True, reconnect=False)
        import struct
        #case 1: // version
        #           return 12; // bpp, size, width, height, 4*(length, offset)
        received = self.__receive(1 * 4 + 12 * 4)
        (version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, glen, aoffset, alen) = struct.unpack('<' + 'L' * 13, received)
        mode = [None]*4
        try:
            mode[roffset/rlen] = 'R'
            mode[boffset/blen] = 'B'
            mode[goffset/glen] = 'G'
            mode[aoffset/alen] = 'A'
        except ZeroDivisionError, ex:
            raise ValueError("Unexpected 0 len in framebuffer description: %s.\nframebuffer: %s" % \
                             (ex, ','.join([str(v) for v in (version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, glen, aoffset, alen)])))
        mode = ''.join(mode)
        if DEBUG:
            print >> sys.stderr, "    takeSnapshot:", (version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, blen, aoffset, alen)
        self.__send('\0', checkok=False, reconnect=False)
        if DEBUG:
            print >> sys.stderr, "    takeSnapshot: reading %d bytes" % (size)
        received = self.__receive(size)
        if reconnect:
            self.__connect()
            self.__setTransport()
        return Image.frombuffer(mode, (width, height), received, 'raw', mode, 0, 1)

    def touch(self, x, y, eventType=DOWN_AND_UP):
        self.shell('input tap %d %d' % (x, y))

    def type(self, text):
        self.shell('input text "%s"' % text)

    def wake(self):
        self.shell('input keyevent 26')

    @staticmethod
    def percentSame(image1, image2):
        '''
        Returns the percent of pixels that are equal

        @author: catshoes
        '''

        # If the images differ in size, return 0% same.
        size_x1, size_y1 = image1.size
        size_x2, size_y2 = image2.size
        if (size_x1 != size_x2 or
            size_y1 != size_y2):
            return 0

        # Images are the same size
        # Return the percent of pixels that are equal.
        numPixelsSame = 0
        numPixelsTotal = size_x1 * size_y1
        image1Pixels = image1.load()
        image2Pixels = image2.load()

        # Loop over all pixels, comparing pixel in image1 to image2
        for x in range(size_x1):
            for y in range(size_y1):
                if (image1Pixels[x, y] == image2Pixels[x, y]):
                    numPixelsSame += 1

        return numPixelsSame / float(numPixelsTotal)

    @staticmethod
    def sameAs(image1, image2, percent=1.0):
        '''
        Compares 2 images

        @author: catshoes
        '''

        return (AdbClient.percentSame(image1, image2) >= percent)

if __name__ == '__main__':
    adbClient = AdbClient(os.environ['ANDROID_SERIAL'])
    INTERACTIVE = False
    if INTERACTIVE:
        sout = adbClient.shell()
        prompt = re.compile(".+@android:(.*) [$#] \r\r\n")
        while True:
            try:
                cmd = raw_input('adb $ ')
            except EOFError:
                break
            if cmd == 'exit':
                break
            adbClient.socket.__send(cmd + "\r\n")
            sout.readline(4096) # eat first line, which is the command
            while True:
                line = sout.readline(4096)
                if prompt.match(line):
                    break
                print line,
                if not line:
                    break

        print "\nBye"
    else:
        print 'date:', adbClient.shell('date')
<commit_msg>Added drag() method to AdbClient<commit_after>'''
Copyright (C) 2012-2013  Diego Torres Milano
Created on Dec 1, 2012

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@author: Diego Torres Milano
'''

__version__ = '4.3.6'

import sys
import socket
import time
import re
import signal
import os
import types
import platform

DEBUG = False

HOSTNAME = 'localhost'
PORT = 5037

OKAY = 'OKAY'
FAIL = 'FAIL'

UP = 0
DOWN = 1
DOWN_AND_UP = 2

TIMEOUT = 15

class Device:
    @staticmethod
    def factory(_str):
        if DEBUG:
            print >> sys.stderr, "Device.factory(", _str, ")"
        values = _str.split(None, 2)
        if DEBUG:
            print >> sys.stderr, "values=", values
        return Device(*values)

    def __init__(self, serialno, status, qualifiers=None):
        self.serialno = serialno
        self.status = status
        self.qualifiers = qualifiers

    def __str__(self):
        return "<<<" + self.serialno + ", " + self.status + ", %s>>>" % self.qualifiers

class AdbClient:

    def __init__(self, serialno, hostname=HOSTNAME, port=PORT, settransport=True, reconnect=True):
        if not serialno:
            raise ValueError("serialno must not be empty or None")
        self.serialno = serialno
        self.hostname = hostname
        self.port = port

        self.reconnect = reconnect
        self.__connect()

        self.checkVersion()
        self.isTransportSet = False
        if settransport:
            self.__setTransport()

    @staticmethod
    def setAlarm(timeout):
        osName = platform.system()
        if osName.startswith('Windows'): # alarm is not implemented in Windows
            return
        if DEBUG:
            print >> sys.stderr, "setAlarm(%d)" % timeout
        signal.alarm(timeout)

    def setReconnect(self, val):
        self.reconnect = val

    def __connect(self):
        if DEBUG:
            print >> sys.stderr, "__connect()"
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.settimeout(TIMEOUT)
        try:
            self.socket.connect((self.hostname, self.port))
        except socket.error, ex:
            raise RuntimeError("ERROR: Connecting to %s:%d: %s" % (self.socket, self.port, ex))

    def close(self):
        if DEBUG:
            print >> sys.stderr, "Closing socket...", self.socket
        if self.socket:
            self.socket.close()

    def __del__(self):
        try:
            self.close()
        except:
            pass

    def __send(self, msg, checkok=True, reconnect=False):
        if DEBUG:
            print >> sys.stderr, "__send(%s, checkok=%s, reconnect=%s)" % (msg, checkok, reconnect)
        if not re.search('^host:', msg):
            if not self.isTransportSet:
                self.__setTransport()
        else:
            self.checkConnected()
        self.socket.send('%04X%s' % (len(msg), msg))
        if checkok:
            self.__checkOk()
        if reconnect:
            if DEBUG:
                print >> sys.stderr, "    __send: reconnecting"
            self.__connect()
            self.__setTransport()

    def __receive(self, nob=None):
        if DEBUG:
            print >> sys.stderr, "__receive()"
        self.checkConnected()
        if nob is None:
            nob = int(self.socket.recv(4), 16)
        if DEBUG:
            print >> sys.stderr, "    __receive: receiving", nob, "bytes"
        recv = bytearray()
        nr = 0
        while nr < nob:
            chunk = self.socket.recv(min((nob-nr), 4096))
            recv.extend(chunk)
            nr += len(chunk)
        if DEBUG:
            print >> sys.stderr, "    __receive: returning len=", len(recv)
        return str(recv)

    def __checkOk(self):
        if DEBUG:
            print >> sys.stderr, "__checkOk()"
        self.checkConnected()
        self.setAlarm(TIMEOUT)
        recv = self.socket.recv(4)
        if DEBUG:
            print >> sys.stderr, "    __checkOk: recv=", repr(recv)
        if recv != OKAY:
            error = self.socket.recv(1024)
            raise RuntimeError("ERROR: %s %s" % (repr(recv), error))
        self.setAlarm(0)
        if DEBUG:
            print >> sys.stderr, "    __checkOk: returning True"
        return True

    def checkConnected(self):
        if DEBUG:
            print >> sys.stderr, "checkConnected()"
        if not self.socket:
            raise RuntimeError("ERROR: Not connected")
        if DEBUG:
            print >> sys.stderr, "    checkConnected: returning True"
        return True

    def checkVersion(self, reconnect=True):
        if DEBUG:
            print >> sys.stderr, "checkVersion(reconnect=%s)" % reconnect
        self.__send('host:version', reconnect=False)
        version = self.socket.recv(8)
        VERSION='0004001f'
        if version != VERSION:
            raise RuntimeError("ERROR: Incorrect ADB server version %s (expecting %s)" % (version, VERSION))
        if reconnect:
            self.__connect()

    def __setTransport(self):
        if DEBUG:
            print >> sys.stderr, "__setTransport()"
        self.checkConnected()
        serialnoRE = re.compile(self.serialno)
        found = False
        for device in self.getDevices():
            if serialnoRE.match(device.serialno):
                found = True
                break
        if not found:
            raise RuntimeError("ERROR: couldn't find device that matches '%s'" % self.serialno)
        self.serialno = device.serialno
        msg = 'host:transport:%s' % self.serialno
        if DEBUG:
            print >> sys.stderr, "    __setTransport: msg=", msg
        self.__send(msg, reconnect=False)
        self.isTransportSet = True

    def getDevices(self):
        if DEBUG:
            print >> sys.stderr, "getDevices()"
        self.__send('host:devices-l', checkok=False)
        try:
            self.__checkOk()
        except RuntimeError, ex:
            print >> sys.stderr, "**ERROR:", ex
            return None
        devices = []
        for line in self.__receive().splitlines():
            devices.append(Device.factory(line))
        self.__connect()
        return devices

    def shell(self, cmd=None):
        if DEBUG:
            print >> sys.stderr, "shell(cmd=%s)" % cmd
        if cmd:
            self.__send('shell:%s' % cmd, checkok=True, reconnect=False)
            out = ''
            while True:
                _str = None
                try:
                    _str = self.socket.recv(4096)
                except Exception, ex:
                    print >> sys.stderr, "ERROR:", ex
                if not _str:
                    break
                out += _str
            if self.reconnect:
                if DEBUG:
                    print >> sys.stderr, "Reconnecting..."
                self.close()
                self.__connect()
                self.__setTransport()
            return out
        else:
            self.__send('shell:')
            #sin = self.socket.makefile("rw")
            #sout = self.socket.makefile("r")
            #return (sin, sin)
            sout = adbClient.socket.makefile("r")
            return sout

    def __getRestrictedScreen(self):
        ''' Gets C{mRestrictedScreen} values from dumpsys. This is a method to obtain display dimensions '''

        rsRE = re.compile('\s*mRestrictedScreen=\((?P<x>\d+),(?P<y>\d+)\) (?P<w>\d+)x(?P<h>\d+)')
        for line in self.shell('dumpsys window').splitlines():
            m = rsRE.match(line)
            if m:
                return m.groups()
        raise RuntimeError("Couldn't find mRestrictedScreen in dumpsys")

    def __getProp(self, key, strip=True):
        prop = self.shell('getprop %s' % key)
        if strip:
            prop = prop.rstrip('\r\n')
        return prop

    def __getDisplayWidth(self, key, strip=True):
        (x, y, w, h) = self.__getRestrictedScreen()
        return int(w)

    def __getDisplayHeight(self, key, strip=True):
        (x, y, w, h) = self.__getRestrictedScreen()
        return int(h)

    def getSystemProperty(self, key, strip=True):
        return self.getProperty(key, strip)

    def getProperty(self, key, strip=True):
        ''' Gets the property value for key '''

        import collections
        MAP_KEYS = collections.OrderedDict([
                          (re.compile('display.width'), self.__getDisplayWidth),
                          (re.compile('display.height'), self.__getDisplayHeight),
                          (re.compile('.*'), self.__getProp),
                          ])
        '''Maps properties key values (as regexps) to instance methods to obtain its values.'''

        for kre in MAP_KEYS.keys():
            if kre.match(key):
                return MAP_KEYS[kre](key=key, strip=strip)
        raise ValueError("key='%s' does not match any map entry")

    def press(self, name, eventType=DOWN_AND_UP):
        cmd = 'input keyevent %s' % name
        if DEBUG:
            print >>sys.stderr, "press(%s)" % cmd
        self.shell(cmd)

    def startActivity(self, component=None, flags=None, uri=None):
        cmd = 'am start'
        if component:
            cmd += ' -n %s' % component
        if flags:
            cmd += ' -f %s' % flags
        if uri:
            cmd += ' %s' % uri
        if DEBUG:
            print >> sys.stderr, "Starting activity: %s" % cmd
        self.shell(cmd)

    def takeSnapshot(self, reconnect=False):
        '''
        Takes a snapshot of the device and return it as a PIL Image.
        '''

        try:
            from PIL import Image
        except:
            raise Exception("You have to install PIL to use takeSnapshot()")
        self.__send('framebuffer:', checkok=True, reconnect=False)
        import struct
        #case 1: // version
        #           return 12; // bpp, size, width, height, 4*(length, offset)
        received = self.__receive(1 * 4 + 12 * 4)
        (version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, glen, aoffset, alen) = struct.unpack('<' + 'L' * 13, received)
        mode = [None]*4
        try:
            mode[roffset/rlen] = 'R'
            mode[boffset/blen] = 'B'
            mode[goffset/glen] = 'G'
            mode[aoffset/alen] = 'A'
        except ZeroDivisionError, ex:
            raise ValueError("Unexpected 0 len in framebuffer description: %s.\nframebuffer: %s" % \
                             (ex, ','.join([str(v) for v in (version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, glen, aoffset, alen)])))
        mode = ''.join(mode)
        if DEBUG:
            print >> sys.stderr, "    takeSnapshot:", (version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, blen, aoffset, alen)
        self.__send('\0', checkok=False, reconnect=False)
        if DEBUG:
            print >> sys.stderr, "    takeSnapshot: reading %d bytes" % (size)
        received = self.__receive(size)
        if reconnect:
            self.__connect()
            self.__setTransport()
        return Image.frombuffer(mode, (width, height), received, 'raw', mode, 0, 1)

    def touch(self, x, y, eventType=DOWN_AND_UP):
        self.shell('input tap %d %d' % (x, y))

    def drag(self, (x0, y0), (x1, y1), duration, steps):
        self.shell('input swipe %d %d %d %d %d' % (x0, y0, x1, y1, duration*1000))

    def type(self, text):
        self.shell('input text "%s"' % text)

    def wake(self):
        self.shell('input keyevent 26')

    @staticmethod
    def percentSame(image1, image2):
        '''
        Returns the percent of pixels that are equal

        @author: catshoes
        '''

        # If the images differ in size, return 0% same.
        size_x1, size_y1 = image1.size
        size_x2, size_y2 = image2.size
        if (size_x1 != size_x2 or
            size_y1 != size_y2):
            return 0

        # Images are the same size
        # Return the percent of pixels that are equal.
        numPixelsSame = 0
        numPixelsTotal = size_x1 * size_y1
        image1Pixels = image1.load()
        image2Pixels = image2.load()

        # Loop over all pixels, comparing pixel in image1 to image2
        for x in range(size_x1):
            for y in range(size_y1):
                if (image1Pixels[x, y] == image2Pixels[x, y]):
                    numPixelsSame += 1

        return numPixelsSame / float(numPixelsTotal)

    @staticmethod
    def sameAs(image1, image2, percent=1.0):
        '''
        Compares 2 images

        @author: catshoes
        '''

        return (AdbClient.percentSame(image1, image2) >= percent)

if __name__ == '__main__':
    adbClient = AdbClient(os.environ['ANDROID_SERIAL'])
    INTERACTIVE = False
    if INTERACTIVE:
        sout = adbClient.shell()
        prompt = re.compile(".+@android:(.*) [$#] \r\r\n")
        while True:
            try:
                cmd = raw_input('adb $ ')
            except EOFError:
                break
            if cmd == 'exit':
                break
            adbClient.socket.__send(cmd + "\r\n")
            sout.readline(4096) # eat first line, which is the command
            while True:
                line = sout.readline(4096)
                if prompt.match(line):
                    break
                print line,
                if not line:
                    break

        print "\nBye"
    else:
        print 'date:', adbClient.shell('date')
<|endoftext|>


<commit_before>from ..baseapi import BaseApi

class AutomationRemovedSubscriber(BaseApi):

    def __init__(self, *args, **kwargs):
        super(AutomationRemovedSubscriber, self).__init__(*args, **kwargs)
        self.endpoint = 'automations'

    def all(self, workflow_id=''):
        """
        Return information about subscribers who have been removed from an Automation workflow.
        """
        return self._mc_client._get(url=self._build_path(workflow_id, 'removed-subscribers'))

    def create(self, workflow_id='', data=None):
        """
        Removes a subscriber from a specific Automation workflow.
        """
        return self._mc_client._post(
            url=self._build_path(workflow_id, 'removed-subscribers'),
            data=data)
<commit_msg>unicode_literals<commit_after>from __future__ import unicode_literals
from ..baseapi import BaseApi

class AutomationRemovedSubscriber(BaseApi):

    def __init__(self, *args, **kwargs):
        super(AutomationRemovedSubscriber, self).__init__(*args, **kwargs)
        self.endpoint = 'automations'

    def all(self, workflow_id=''):
        """
        Return information about subscribers who have been removed from an Automation workflow.
        """
        return self._mc_client._get(url=self._build_path(workflow_id, 'removed-subscribers'))

    def create(self, workflow_id='', data=None):
        """
        Removes a subscriber from a specific Automation workflow.
        """
        return self._mc_client._post(
            url=self._build_path(workflow_id, 'removed-subscribers'),
            data=data)
<|endoftext|>


<commit_before># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import six
import datetime

from st2common.constants.triggers import TRIGGER_INSTANCE_PROCESSED
from st2common.transport.publishers import PoolPublisher
from st2common.persistence.trigger import TriggerInstance
from st2common.models.db.trigger import TriggerInstanceDB
from st2common.util import date as date_utils
from st2api.controllers.v1.triggers import TriggerInstanceController

from tests.base import FunctionalTest
from tests.base import APIControllerWithIncludeAndExcludeFilterTestCase

http_client = six.moves.http_client

@mock.patch.object(PoolPublisher, 'publish', mock.MagicMock())
class TriggerInstanceTestCase(FunctionalTest,
                              APIControllerWithIncludeAndExcludeFilterTestCase):
    get_all_path = '/v1/triggerinstances'
    controller_cls = TriggerInstanceController
    include_attribute_field_name = 'trigger'
    exclude_attribute_field_name = 'payload'

    @classmethod
    def setUpClass(cls):
        super(TriggerInstanceTestCase, cls).setUpClass()
        cls._setupTriggerTypes()
        cls._setupTriggers()
        cls._setupTriggerInstance()

    def test_get_all(self):
        resp = self.app.get('/v1/triggerinstances')
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), self.triggerinstance_count, 'Get all failure.')

    def test_get_all_limit(self):
        limit = 1
        resp = self.app.get('/v1/triggerinstances?limit=%d' % limit)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), limit, 'Get all failure. Length doesn\'t match limit.')

    def test_get_all_limit_negative_number(self):
        limit = -22
        resp = self.app.get('/v1/triggerinstances?limit=%d' % limit, expect_errors=True)
        self.assertEqual(resp.status_int, 400)
        self.assertEqual(resp.json['faultstring'],
                         u'Limit, "-22" specified, must be a positive number.')

    def test_get_all_filter_by_trigger(self):
        trigger = 'dummy_pack_1.st2.test.trigger0'
        resp = self.app.get('/v1/triggerinstances?trigger=%s' % trigger)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), 1, 'Get all failure. Must get only one such instance.')

    def test_get_all_filter_by_timestamp(self):
        resp = self.app.get('/v1/triggerinstances')
        self.assertEqual(resp.status_int, http_client.OK)
        timestamp_largest = resp.json[0]['occurrence_time']
        timestamp_middle = resp.json[1]['occurrence_time']
        resp = self.app.get('/v1/triggerinstances?timestamp_gt=%s' % timestamp_largest)
        # Since we sort trigger instances by time (latest first), the previous
        # get should return no trigger instances.
        self.assertEqual(len(resp.json), 0)
        resp = self.app.get('/v1/triggerinstances?timestamp_lt=%s' % (timestamp_middle))
        self.assertEqual(len(resp.json), 1)

    def test_get_all_trigger_type_ref_filtering(self):
        # 1. Invalid / inexistent trigger type ref
        resp = self.app.get('/v1/triggerinstances?trigger_type=foo.bar.invalid')
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), 0)

        # 2. Valid trigger type ref with corresponding trigger instances
        resp = self.app.get('/v1/triggerinstances?trigger_type=dummy_pack_1.st2.test.triggertype0')
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), 1)

        # 3. Valid trigger type ref with no corresponding trigger instances
        resp = self.app.get('/v1/triggerinstances?trigger_type=dummy_pack_1.st2.test.triggertype3')
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), 0)

    def test_reemit_trigger_instance(self):
        resp = self.app.get('/v1/triggerinstances')
        self.assertEqual(resp.status_int, http_client.OK)
        instance_id = resp.json[0]['id']
        resp = self.app.post('/v1/triggerinstances/%s/re_emit' % instance_id)
        self.assertEqual(resp.status_int, http_client.OK)
        resent_message = resp.json['message']
        resent_payload = resp.json['payload']
        self.assertTrue(instance_id in resent_message)
        self.assertTrue('__context' in resent_payload)
        self.assertEqual(resent_payload['__context']['original_id'], instance_id)

    def test_get_one(self):
        triggerinstance_id = str(self.triggerinstance_1.id)
        resp = self._do_get_one(triggerinstance_id)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(self._get_id(resp), triggerinstance_id)

        triggerinstance_id = str(self.triggerinstance_2.id)
        resp = self._do_get_one(triggerinstance_id)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(self._get_id(resp), triggerinstance_id)

        triggerinstance_id = str(self.triggerinstance_3.id)
        resp = self._do_get_one(triggerinstance_id)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(self._get_id(resp), triggerinstance_id)

    def test_get_one_fail(self):
        resp = self._do_get_one('1')
        self.assertEqual(resp.status_int, http_client.NOT_FOUND)

    @classmethod
    def _setupTriggerTypes(cls):
        TRIGGERTYPE_0 = {
            'name': 'st2.test.triggertype0',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'payload_schema': {'tp1': None, 'tp2': None, 'tp3': None},
            'parameters_schema': {}
        }
        TRIGGERTYPE_1 = {
            'name': 'st2.test.triggertype1',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'payload_schema': {'tp1': None, 'tp2': None, 'tp3': None},
        }
        TRIGGERTYPE_2 = {
            'name': 'st2.test.triggertype2',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'payload_schema': {'tp1': None, 'tp2': None, 'tp3': None},
            'parameters_schema': {'param1': {'type': 'object'}}
        }
        TRIGGERTYPE_3 = {
            'name': 'st2.test.triggertype3',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'payload_schema': {'tp1': None, 'tp2': None, 'tp3': None},
            'parameters_schema': {'param1': {'type': 'object'}}
        }
        cls.app.post_json('/v1/triggertypes', TRIGGERTYPE_0, expect_errors=False)
        cls.app.post_json('/v1/triggertypes', TRIGGERTYPE_1, expect_errors=False)
        cls.app.post_json('/v1/triggertypes', TRIGGERTYPE_2, expect_errors=False)
        cls.app.post_json('/v1/triggertypes', TRIGGERTYPE_3, expect_errors=False)

    @classmethod
    def _setupTriggers(cls):
        TRIGGER_0 = {
            'name': 'st2.test.trigger0',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'type': 'dummy_pack_1.st2.test.triggertype0',
            'parameters': {}
        }

        TRIGGER_1 = {
            'name': 'st2.test.trigger1',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'type': 'dummy_pack_1.st2.test.triggertype1',
            'parameters': {}
        }

        TRIGGER_2 = {
            'name': 'st2.test.trigger2',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'type': 'dummy_pack_1.st2.test.triggertype2',
            'parameters': {
                'param1': {
                    'foo': 'bar'
                }
            }
        }
        cls.app.post_json('/v1/triggers', TRIGGER_0, expect_errors=False)
        cls.app.post_json('/v1/triggers', TRIGGER_1, expect_errors=False)
        cls.app.post_json('/v1/triggers', TRIGGER_2, expect_errors=False)

    def _insert_mock_models(self):
        return [self.triggerinstance_1['id'], self.triggerinstance_2['id'],
                self.triggerinstance_3['id']]

    def _delete_mock_models(self, object_ids):
        return None

    @classmethod
    def _setupTriggerInstance(cls):
        cls.triggerinstance_count = 0
        cls.triggerinstance_1 = cls._create_trigger_instance(
            trigger_ref='dummy_pack_1.st2.test.trigger0',
            payload={'tp1': 1, 'tp2': 2, 'tp3': 3},
            seconds=1)
        cls.triggerinstance_2 = cls._create_trigger_instance(
            trigger_ref='dummy_pack_1.st2.test.trigger1',
            payload={'tp1': 'a', 'tp2': 'b', 'tp3': 'c'},
            seconds=2)
        cls.triggerinstance_3 = cls._create_trigger_instance(
            trigger_ref='dummy_pack_1.st2.test.trigger2',
            payload={'tp1': None, 'tp2': None, 'tp3': None},
            seconds=3)

    @classmethod
    def _create_trigger_instance(cls, trigger_ref, payload, seconds):
        # Note: We use 1 second intervals between occurence time to prevent
        # occasional test failures
        occurrence_time = date_utils.get_datetime_utc_now()
        occurrence_time = occurrence_time + datetime.timedelta(seconds=seconds)

        trigger_instance = TriggerInstanceDB()
        trigger_instance.trigger = trigger_ref
        trigger_instance.payload = payload
        trigger_instance.occurrence_time = occurrence_time
        trigger_instance.status = TRIGGER_INSTANCE_PROCESSED
        created = TriggerInstance.add_or_update(trigger_instance)
        cls.triggerinstance_count += 1
        return created

    @staticmethod
    def _get_id(resp):
        return resp.json['id']

    def _do_get_one(self, triggerinstance_id):
        return self.app.get('/v1/triggerinstances/%s' % triggerinstance_id, expect_errors=True)
<commit_msg>Make the test more robust / correct.<commit_after># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import six
import datetime

from st2common.constants.triggers import TRIGGER_INSTANCE_PROCESSED
from st2common.transport.publishers import PoolPublisher
from st2common.persistence.trigger import TriggerInstance
from st2common.models.db.trigger import TriggerInstanceDB
from st2common.util import date as date_utils
from st2common.util import isotime
from st2api.controllers.v1.triggers import TriggerInstanceController

from tests.base import FunctionalTest
from tests.base import APIControllerWithIncludeAndExcludeFilterTestCase

http_client = six.moves.http_client

@mock.patch.object(PoolPublisher, 'publish', mock.MagicMock())
class TriggerInstanceTestCase(FunctionalTest,
                              APIControllerWithIncludeAndExcludeFilterTestCase):
    get_all_path = '/v1/triggerinstances'
    controller_cls = TriggerInstanceController
    include_attribute_field_name = 'trigger'
    exclude_attribute_field_name = 'payload'

    @classmethod
    def setUpClass(cls):
        super(TriggerInstanceTestCase, cls).setUpClass()
        cls._setupTriggerTypes()
        cls._setupTriggers()
        cls._setupTriggerInstance()

    def test_get_all(self):
        resp = self.app.get('/v1/triggerinstances')
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), self.triggerinstance_count, 'Get all failure.')

    def test_get_all_limit(self):
        limit = 1
        resp = self.app.get('/v1/triggerinstances?limit=%d' % limit)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), limit, 'Get all failure. Length doesn\'t match limit.')

    def test_get_all_limit_negative_number(self):
        limit = -22
        resp = self.app.get('/v1/triggerinstances?limit=%d' % limit, expect_errors=True)
        self.assertEqual(resp.status_int, 400)
        self.assertEqual(resp.json['faultstring'],
                         u'Limit, "-22" specified, must be a positive number.')

    def test_get_all_filter_by_trigger(self):
        trigger = 'dummy_pack_1.st2.test.trigger0'
        resp = self.app.get('/v1/triggerinstances?trigger=%s' % trigger)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), 1, 'Get all failure. Must get only one such instance.')

    def test_get_all_filter_by_timestamp(self):
        resp = self.app.get('/v1/triggerinstances')
        self.assertEqual(resp.status_int, http_client.OK)
        timestamp_largest = resp.json[0]['occurrence_time']
        timestamp_middle = resp.json[1]['occurrence_time']

        dt = isotime.parse(timestamp_largest)
        dt = dt + datetime.timedelta(seconds=1)
        timestamp_largest = isotime.format(dt, offset=False)

        resp = self.app.get('/v1/triggerinstances?timestamp_gt=%s' % timestamp_largest)
        # Since we sort trigger instances by time (latest first), the previous
        # get should return no trigger instances.
        self.assertEqual(len(resp.json), 0)

        resp = self.app.get('/v1/triggerinstances?timestamp_lt=%s' % (timestamp_middle))
        self.assertEqual(len(resp.json), 1)

    def test_get_all_trigger_type_ref_filtering(self):
        # 1. Invalid / inexistent trigger type ref
        resp = self.app.get('/v1/triggerinstances?trigger_type=foo.bar.invalid')
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), 0)

        # 2. Valid trigger type ref with corresponding trigger instances
        resp = self.app.get('/v1/triggerinstances?trigger_type=dummy_pack_1.st2.test.triggertype0')
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), 1)

        # 3. Valid trigger type ref with no corresponding trigger instances
        resp = self.app.get('/v1/triggerinstances?trigger_type=dummy_pack_1.st2.test.triggertype3')
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(len(resp.json), 0)

    def test_reemit_trigger_instance(self):
        resp = self.app.get('/v1/triggerinstances')
        self.assertEqual(resp.status_int, http_client.OK)
        instance_id = resp.json[0]['id']
        resp = self.app.post('/v1/triggerinstances/%s/re_emit' % instance_id)
        self.assertEqual(resp.status_int, http_client.OK)
        resent_message = resp.json['message']
        resent_payload = resp.json['payload']
        self.assertTrue(instance_id in resent_message)
        self.assertTrue('__context' in resent_payload)
        self.assertEqual(resent_payload['__context']['original_id'], instance_id)

    def test_get_one(self):
        triggerinstance_id = str(self.triggerinstance_1.id)
        resp = self._do_get_one(triggerinstance_id)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(self._get_id(resp), triggerinstance_id)

        triggerinstance_id = str(self.triggerinstance_2.id)
        resp = self._do_get_one(triggerinstance_id)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(self._get_id(resp), triggerinstance_id)

        triggerinstance_id = str(self.triggerinstance_3.id)
        resp = self._do_get_one(triggerinstance_id)
        self.assertEqual(resp.status_int, http_client.OK)
        self.assertEqual(self._get_id(resp), triggerinstance_id)

    def test_get_one_fail(self):
        resp = self._do_get_one('1')
        self.assertEqual(resp.status_int, http_client.NOT_FOUND)

    @classmethod
    def _setupTriggerTypes(cls):
        TRIGGERTYPE_0 = {
            'name': 'st2.test.triggertype0',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'payload_schema': {'tp1': None, 'tp2': None, 'tp3': None},
            'parameters_schema': {}
        }
        TRIGGERTYPE_1 = {
            'name': 'st2.test.triggertype1',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'payload_schema': {'tp1': None, 'tp2': None, 'tp3': None},
        }
        TRIGGERTYPE_2 = {
            'name': 'st2.test.triggertype2',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'payload_schema': {'tp1': None, 'tp2': None, 'tp3': None},
            'parameters_schema': {'param1': {'type': 'object'}}
        }
        TRIGGERTYPE_3 = {
            'name': 'st2.test.triggertype3',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'payload_schema': {'tp1': None, 'tp2': None, 'tp3': None},
            'parameters_schema': {'param1': {'type': 'object'}}
        }
        cls.app.post_json('/v1/triggertypes', TRIGGERTYPE_0, expect_errors=False)
        cls.app.post_json('/v1/triggertypes', TRIGGERTYPE_1, expect_errors=False)
        cls.app.post_json('/v1/triggertypes', TRIGGERTYPE_2, expect_errors=False)
        cls.app.post_json('/v1/triggertypes', TRIGGERTYPE_3, expect_errors=False)

    @classmethod
    def _setupTriggers(cls):
        TRIGGER_0 = {
            'name': 'st2.test.trigger0',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'type': 'dummy_pack_1.st2.test.triggertype0',
            'parameters': {}
        }

        TRIGGER_1 = {
            'name': 'st2.test.trigger1',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'type': 'dummy_pack_1.st2.test.triggertype1',
            'parameters': {}
        }

        TRIGGER_2 = {
            'name': 'st2.test.trigger2',
            'pack': 'dummy_pack_1',
            'description': 'test trigger',
            'type': 'dummy_pack_1.st2.test.triggertype2',
            'parameters': {
                'param1': {
                    'foo': 'bar'
                }
            }
        }
        cls.app.post_json('/v1/triggers', TRIGGER_0, expect_errors=False)
        cls.app.post_json('/v1/triggers', TRIGGER_1, expect_errors=False)
        cls.app.post_json('/v1/triggers', TRIGGER_2, expect_errors=False)

    def _insert_mock_models(self):
        return [self.triggerinstance_1['id'], self.triggerinstance_2['id'],
                self.triggerinstance_3['id']]

    def _delete_mock_models(self, object_ids):
        return None

    @classmethod
    def _setupTriggerInstance(cls):
        cls.triggerinstance_count = 0
        cls.triggerinstance_1 = cls._create_trigger_instance(
            trigger_ref='dummy_pack_1.st2.test.trigger0',
            payload={'tp1': 1, 'tp2': 2, 'tp3': 3},
            seconds=1)
        cls.triggerinstance_2 = cls._create_trigger_instance(
            trigger_ref='dummy_pack_1.st2.test.trigger1',
            payload={'tp1': 'a', 'tp2': 'b', 'tp3': 'c'},
            seconds=2)
        cls.triggerinstance_3 = cls._create_trigger_instance(
            trigger_ref='dummy_pack_1.st2.test.trigger2',
            payload={'tp1': None, 'tp2': None, 'tp3': None},
            seconds=3)

    @classmethod
    def _create_trigger_instance(cls, trigger_ref, payload, seconds):
        # Note: We use 1 second intervals between occurence time to prevent
        # occasional test failures
        occurrence_time = date_utils.get_datetime_utc_now()
        occurrence_time = occurrence_time + datetime.timedelta(seconds=seconds)

        trigger_instance = TriggerInstanceDB()
        trigger_instance.trigger = trigger_ref
        trigger_instance.payload = payload
        trigger_instance.occurrence_time = occurrence_time
        trigger_instance.status = TRIGGER_INSTANCE_PROCESSED
        created = TriggerInstance.add_or_update(trigger_instance)
        cls.triggerinstance_count += 1
        return created

    @staticmethod
    def _get_id(resp):
        return resp.json['id']

    def _do_get_one(self, triggerinstance_id):
        return self.app.get('/v1/triggerinstances/%s' % triggerinstance_id, expect_errors=True)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright 2016 Eficent Business and IT Consulting Services S.L.
#   (http://www.eficent.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from openerp import api, fields, models, _
from openerp.exceptions import Warning as UserError

class StockWarehouseOrderpoint(models.Model):
    _inherit = "stock.warehouse.orderpoint"

    procure_uom_id = fields.Many2one(comodel_name='product.uom',
                                     string="Procurement UoM")

    @api.constrains('product_uom', 'procure_uom_id')
    def _check_procure_uom(self):
        if any(orderpoint.product_uom and
                orderpoint.procure_uom_id and
                orderpoint.product_uom.category_id !=
                orderpoint.procure_uom_id.category_id
                for orderpoint in self):
                    raise UserError(
                        _('Error: The product default Unit of Measure and '
                          'the procurement Unit of Measure must be in the '
                          'same category.'))
        return True
<commit_msg>Warning deprecated.<commit_after># -*- coding: utf-8 -*-
# Copyright 2016 Eficent Business and IT Consulting Services S.L.
#   (http://www.eficent.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from openerp import api, fields, models, _
from openerp.exceptions import UserError

class StockWarehouseOrderpoint(models.Model):
    _inherit = "stock.warehouse.orderpoint"

    procure_uom_id = fields.Many2one(comodel_name='product.uom',
                                     string="Procurement UoM")

    @api.constrains('product_uom', 'procure_uom_id')
    def _check_procure_uom(self):
        if any(orderpoint.product_uom and
                orderpoint.procure_uom_id and
                orderpoint.product_uom.category_id !=
                orderpoint.procure_uom_id.category_id
                for orderpoint in self):
                    raise UserError(
                        _('Error: The product default Unit of Measure and '
                          'the procurement Unit of Measure must be in the '
                          'same category.'))
        return True
<|endoftext|>