@handler.asynchronous(datastore=True, obj_store=True)
  @gen.coroutine
  def post(self, action):

    if not ServerEnvironment.IsDevBox():
      raise web.HTTPError(403, _TEST_HOOKS_NOT_SUPPORTED)

    from PIL import Image, ImageChops

    if action == 'copy':
      logging.info('Updating baseline image')
      urls = {}
      body = json.loads(self.request.body)
      testname = body['testname']
      imagename = body['imagename']
      scheme = body['scheme']

      _FULL_RESULTS_BASELINE = '%s/results/baseline/%s' % (options.options.testing_path, scheme)
      _FULL_RESULTS_CURRENT = (options.options.testing_path, scheme) % '%s/results/current/%s'

      # Overwrite the 'baseline' image for the test with the 'current' image.
      baseline_image = r'%s/%s/%s' % (_FULL_RESULTS_BASELINE, testname, imagename)
      current_image = r'%s/%s/Run 1/%s' % (_FULL_RESULTS_CURRENT, testname, imagename)

      yield self._UpdateImageMaskConfig(testname, imagename, scheme)

      if os.path.exists(current_image):
        shutil.copy(current_image, baseline_image)
        logging.info('Updated baseline image for %s' % testname)

      baseline_web_image = r'%s/%s/%s/%s' % (_STATIC_RESULTS_BASELINE, scheme, testname, imagename)
      current_web_image = r'%s/%s/%s/Run 1/%s' % (_STATIC_RESULTS_CURRENT, scheme, testname, imagename)

      urls['baseline'] = baseline_web_image
      urls['current'] = current_web_image

      # Return JSON result.
      self.write(urls)
      self.finish()
      return

    if action == 'delete':
      body = json.loads(self.request.body)
      testname = body['testname']
      imagename = body['imagename']

      current_image = r'%s/%s/Run 1/%s' % (_FULL_RESULTS_CURRENT, testname, imagename)
      if os.path.exists(current_image) is True:
        os.remove(current_image)
        logging.info('Deleted current capture image for %s' % testname)
      self.finish()
      return

    if action == 'token':
      body = json.loads(self.request.body)
      identity_key = body['auth_info']['identity'];

      identity = yield gen.Task(Identity.Query, self._client, identity_key, None, must_exist=False)
      if identity is None:
        raise web.HTTPError(400, 'Identity does not exist.')

      self.write(identity.access_token)
      self.finish()
      return

    if action == 'image':
      body = json.loads(self.request.body)
      test_name = body['testname']
      image_name = body['imagename']
      scheme = body['scheme']
      _FULL_RESULTS_BASELINE = '%s/results/baseline/%s' % (options.options.testing_path, scheme)
      _FULL_RESULTS_CURRENT = '%s/results/current/%s' % (options.options.testing_path, scheme)

      # get image base name
      tmp = image_name[:-4]
      base, num = tmp.split('|', 1)
      image_base_name = '%s.png' % base

      image1 = r'%s/%s/%s' % (_FULL_RESULTS_BASELINE, test_name, image_base_name)
      image2 = r'%s/%s/Run 1/%s' % (_FULL_RESULTS_CURRENT, test_name, image_name)

      if os.path.exists(image1) and os.path.exists(image2):
        self.set_header('Content-Type', 'application/json; charset=UTF-8')
        im1 = Image.open(image1)
        im2 = Image.open(image2)

        diff = ImageChops.difference(im2, im1)
        result = diff.getbbox() is None
        response = { 'response': result, 'bbox': diff.getbbox() }
        self.write(response)
      self.finish()
      return

    raise web.HTTPError(400, _TEST_ACTION_NOT_SUPPORTED)
dataset/ETHPy150Open viewfinderco/viewfinder/backend/www/test_hook.py/TestHookHandler.post/swapped operands of `%`