<commit_before>#!/usr/bin/env python3

"""Copyright (c) 2019 Chaquo Ltd. All rights reserved."""

# Keep valid Python 2 syntax so we can produce an error message.
from __future__ import absolute_import, division, print_function

# Do this as early as possible to minimize the chance of something else going wrong and causing
# a less comprehensible error message.
from .util import check_build_python
check_build_python()

import argparse
import importlib.util
import os
from os.path import abspath, join
import py_compile
import shutil
import sys

# See importlib._bootstrap_external.MAGIC_NUMBER.
EXPECTED_MAGIC_NUMBER = (3413).to_bytes(2, 'little') + b'\r\n'

def main():
    args = parse_args()
    if importlib.util.MAGIC_NUMBER != EXPECTED_MAGIC_NUMBER:
        if args.warning:
            # Causes Android Studio to show the line as a warning in tree view.
            print("Warning: ", end="")
        print("buildPython version is {}.{}.{}".format(*sys.version_info[:3]),
              "with bytecode magic number {}".format(importlib.util.MAGIC_NUMBER.hex()),
              "(expected number is {})".format(EXPECTED_MAGIC_NUMBER.hex()))
        sys.exit(1)

    # py_compile uses _bootstrap_external._write_atomic, which writes to a temporary file with
    # a longer name, potentially pushing us over the Windows 260-character filename limit. But
    # since there are no other processes accessing the directory, atomicity doesn't matter.
    try:
        from importlib import _bootstrap_external
        if hasattr(_bootstrap_external, "_write_atomic"):
            def _write_atomic_override(path, data, mode=0o666):
                with open(path, "wb") as f:
                    f.write(data)
                os.chmod(path, mode)
            _bootstrap_external._write_atomic = _write_atomic_override
        else:
            print("Warning: importlib._bootstrap_external._write_atomic doesn't exist")
    except ImportError:
        print("Warning: importlib._bootstrap_external doesn't exist")

    os.chdir(args.in_dir)
    for dirpath, dirnames, filenames in os.walk(os.getcwd()):
        os.makedirs(join(args.out_dir, dirpath), exist_ok=True)
        for filename in filenames:
            in_filename = join(dirpath, filename)
            out_filename = join(args.out_dir, in_filename)
            compiled = False
            if filename.endswith(".py"):
                try:
                    py_compile.compile(in_filename, out_filename + "c", doraise=True)
                    compiled = True
                except py_compile.PyCompileError as e:
                    if not args.quiet:
                        print(e)
            if (not compiled) and (args.in_dir != args.out_dir):
                shutil.copy(in_filename, out_filename)

def parse_args():
    ap = argparse.ArgumentParser()
    ap.add_argument("--quiet", action="store_true")
    ap.add_argument("--warning", action="store_true")
    ap.add_argument("in_dir", type=abspath)
    ap.add_argument("out_dir", type=abspath, nargs="?")
    args = ap.parse_args()
    if args.out_dir is None:
        args.out_dir = args.in_dir
    return args

if __name__ == "__main__":
    main()
<commit_msg>Suppress SyntaxWarning when running py_compile (closes #5621)<commit_after>#!/usr/bin/env python3

"""Copyright (c) 2019 Chaquo Ltd. All rights reserved."""

# Keep valid Python 2 syntax so we can produce an error message.
from __future__ import absolute_import, division, print_function

# Do this as early as possible to minimize the chance of something else going wrong and causing
# a less comprehensible error message.
from .util import check_build_python
check_build_python()

import argparse
import importlib.util
import os
from os.path import abspath, join
import py_compile
import shutil
import sys
import warnings

# See importlib._bootstrap_external.MAGIC_NUMBER.
EXPECTED_MAGIC_NUMBER = (3413).to_bytes(2, 'little') + b'\r\n'

def main():
    args = parse_args()
    if importlib.util.MAGIC_NUMBER != EXPECTED_MAGIC_NUMBER:
        if args.warning:
            # Causes Android Studio to show the line as a warning in tree view.
            print("Warning: ", end="")
        print("buildPython version is {}.{}.{}".format(*sys.version_info[:3]),
              "with bytecode magic number {}".format(importlib.util.MAGIC_NUMBER.hex()),
              "(expected number is {})".format(EXPECTED_MAGIC_NUMBER.hex()))
        sys.exit(1)

    if args.quiet:
        warnings.filterwarnings("ignore", category=SyntaxWarning)

    # py_compile uses _bootstrap_external._write_atomic, which writes to a temporary file with
    # a longer name, potentially pushing us over the Windows 260-character filename limit. But
    # since there are no other processes accessing the directory, atomicity doesn't matter.
    try:
        from importlib import _bootstrap_external
        if hasattr(_bootstrap_external, "_write_atomic"):
            def _write_atomic_override(path, data, mode=0o666):
                with open(path, "wb") as f:
                    f.write(data)
                os.chmod(path, mode)
            _bootstrap_external._write_atomic = _write_atomic_override
        else:
            print("Warning: importlib._bootstrap_external._write_atomic doesn't exist")
    except ImportError:
        print("Warning: importlib._bootstrap_external doesn't exist")

    os.chdir(args.in_dir)
    for dirpath, dirnames, filenames in os.walk(os.getcwd()):
        os.makedirs(join(args.out_dir, dirpath), exist_ok=True)
        for filename in filenames:
            in_filename = join(dirpath, filename)
            out_filename = join(args.out_dir, in_filename)
            compiled = False
            if filename.endswith(".py"):
                try:
                    py_compile.compile(in_filename, out_filename + "c", doraise=True)
                    compiled = True
                except py_compile.PyCompileError as e:
                    if not args.quiet:
                        print(e)
            if (not compiled) and (args.in_dir != args.out_dir):
                shutil.copy(in_filename, out_filename)

def parse_args():
    ap = argparse.ArgumentParser()
    ap.add_argument("--quiet", action="store_true")
    ap.add_argument("--warning", action="store_true")
    ap.add_argument("in_dir", type=abspath)
    ap.add_argument("out_dir", type=abspath, nargs="?")
    args = ap.parse_args()
    if args.out_dir is None:
        args.out_dir = args.in_dir
    return args

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before># system
import os
import re
import shutil
import logging
import logging.handlers
# scientific
import pandas as pd
# custom
from data import PdfSlider
from emailer import Emailer

class ExpCommon(object):
  def __init__(self, root, name):
    """
    `root`: file root
    `name`: project name
    """
    self.root = root
    self.name = name
    self.comp = 6
    self.upass = None
    self._init_logger()

  def _init_logger(self):
    cn = self._underscore(self.__class__.__name__)
    fmt = logging. \
        Formatter(fmt='%(asctime)s,%(levelname)s,' +
                  '%(name)s,%(funcName)s,%(lineno)d, %(message)s',
                  datefmt='%m/%d/%Y %H:%M:%S')
    logger = logging.getLogger("{}.{}.{}.{}".
                               format(__name__, cn, self.root, self.name))
    logger.setLevel(logging.INFO)
    logger.propagate = 0
    # FileHandler
    fn = self.make_path('log', 'log', True, False)
    fnh = fn + "_fh"
    cnh = fn + "_ch"
    if fnh not in [lh.name for lh in logger.handlers]:
      fh = logging.handlers. \
          RotatingFileHandler(fn, maxBytes=10485760, backupCount=5)
      fh.name = fnh
      fh.setFormatter(fmt)
      logger.addHandler(fh)
    # StreamHandler
    if 0 and cnh not in [lh.name for lh in logger.handlers]:
      ch = logging.StreamHandler()
      ch.name = cnh
      ch.setFormatter(fmt)
      logger.addHandler(ch)
    logger.info(">>============== {} inited ================= <<".format(cn))
    self.log = logger

  def _save_key(self, fpath, key):
    kf = self.load('keys')
    if kf is None:
      kf = pd.DataFrame([key], columns=['key'])
    else:
      kf = kf.append(pd.DataFrame([key], columns=['key']))
      kf = kf.reset_index()
      for rc in ['index', 'level']:
        if rc in kf.columns:
          del kf[rc]
    kf.to_hdf(fpath, 'keys', mode='a', data_columns=True,
              format='t', complib='blosc', complevel=self.comp)

  def _underscore(self, string=''):
    # move pre-compile out the loop to improve performance
    first_cap_re = re.compile('(.)([A-Z][a-z]+)')
    all_cap_re = re.compile('([a-z0-9])([A-Z])')
    s1 = first_cap_re.sub(r'\1_\2', string)
    return all_cap_re.sub(r'\1_\2', s1).lower()

  def _ustr_dict(self, di):
    """
    Make dict keys underscore for saving to hdfs
    """
    s = ""
    ks = di.keys()
    ks.sort()
    for k in ks:
      s += "/{}_{}".format(k, di[k])
    return s

  def _asure_path(self, path):
    if not os.path.exists(path):
      os.makedirs(path)

  def slide_pages(self):
    ps = PdfSlider(self.name, self.root)
    return ps.pages()

  def slides_path(self, size='mid'):
    ps = PdfSlider(self.name, self.root)
    return ps.slides_path(size)

  def store(self):
    sp = self.store_path()
    return pd.HDFStore(sp, format='t', data_columns=True,
                       complib='blosc', complevel=self.comp)

  def make_path(self, resource='stores', ext='h5', asure=True, root=False):
    """
    resource: resource name in path
    ext: file extension for resource, if None then return only path
    asure: make sure the path exist
    root: get only root path of given resource
    usage:
      # make simple log path, and make sure created
      self.make_path('log', None)
      # make path for log with log extension, and not check existence
      self.make_path('log', 'log', False)
    """
    rt = self.root
    pn = self.name
    cn = self._underscore(self.__class__.__name__)
    pth = "data/{}/{}/{}".format(rt, pn, resource)
    if root:
      return pth
    if asure:
      self._asure_path(pth)
    pth = "{}/{}".format(pth, cn)
    if ext is not None:
      pth = "{}.{}".format(pth, ext)
    return pth

  def store_path(self):
    """
    DEPRECATED, use make_path instead.
    """
    return self.make_path()

  def delete_file(self, tar=[('store', 'h5', False)]):
    for res, ext, root in tar:
      ph = self.make_path(res, ext, False, root)
      print ph
      if ext is None or root:  # for whole directory
        shutil.rmtree(ph)
      else:  # for a single file
        if os.path.isfile(ph):
          os.remove(ph)

  def save(self, key, data):
    self.log.info('save key ==> {}'.format(key))
    sp = self.make_path('stores', 'h5', asure=True, root=False)
    data.to_hdf(sp, key, mode='a', data_columns=True, format='t',
                complib='blosc', complevel=self.comp)
    self._save_key(sp, key)

  def load(self, key):
    sp = self.store_path()
    try:
      df = pd.read_hdf(sp, key, format='t')
    except KeyError, e:
      print e
      return None
    return df

  def notify(self, summary):
    if self.upass is None:
        return
    ps = self.upass
    cn = self._underscore(self.__class__.__name__)
    title = "Pyslider Job: {} <{}-{}> Finished". \
        format(cn, self.root, self.name)
    me = "speed.of.lightt@gmail.com"
    with Emailer(config=dict(uname=me, upass=ps)) as mailer:
      mailer.send(title, summary, ['speed.of.lightt@gmail.com'])
<commit_msg>Rename PdfSlider to Slider in exp.exp_common<commit_after># system
import os
import re
import shutil
import logging
import logging.handlers
# scientific
import pandas as pd
# custom
from summary import Slider
from emailer import Emailer

class ExpCommon(object):
  def __init__(self, root, name):
    """
    `root`: file root
    `name`: project name
    """
    self.root = root
    self.name = name
    self.comp = 6
    self.upass = None
    self._init_logger()

  def _init_logger(self):
    cn = self._underscore(self.__class__.__name__)
    fmt = logging. \
        Formatter(fmt='%(asctime)s,%(levelname)s,' +
                  '%(name)s,%(funcName)s,%(lineno)d, %(message)s',
                  datefmt='%m/%d/%Y %H:%M:%S')
    logger = logging.getLogger("{}.{}.{}.{}".
                               format(__name__, cn, self.root, self.name))
    logger.setLevel(logging.INFO)
    logger.propagate = 0
    # FileHandler
    fn = self.make_path('log', 'log', True, False)
    fnh = fn + "_fh"
    cnh = fn + "_ch"
    if fnh not in [lh.name for lh in logger.handlers]:
      fh = logging.handlers. \
          RotatingFileHandler(fn, maxBytes=10485760, backupCount=5)
      fh.name = fnh
      fh.setFormatter(fmt)
      logger.addHandler(fh)
    # StreamHandler
    if 0 and cnh not in [lh.name for lh in logger.handlers]:
      ch = logging.StreamHandler()
      ch.name = cnh
      ch.setFormatter(fmt)
      logger.addHandler(ch)
    logger.info(">>============== {} inited ================= <<".format(cn))
    self.log = logger

  def _save_key(self, fpath, key):
    kf = self.load('keys')
    if kf is None:
      kf = pd.DataFrame([key], columns=['key'])
    else:
      kf = kf.append(pd.DataFrame([key], columns=['key']))
      kf = kf.reset_index()
      for rc in ['index', 'level']:
        if rc in kf.columns:
          del kf[rc]
    kf.to_hdf(fpath, 'keys', mode='a', data_columns=True,
              format='t', complib='blosc', complevel=self.comp)

  def _underscore(self, string=''):
    # move pre-compile out the loop to improve performance
    first_cap_re = re.compile('(.)([A-Z][a-z]+)')
    all_cap_re = re.compile('([a-z0-9])([A-Z])')
    s1 = first_cap_re.sub(r'\1_\2', string)
    return all_cap_re.sub(r'\1_\2', s1).lower()

  def _ustr_dict(self, di):
    """
    Make dict keys underscore for saving to hdfs
    """
    s = ""
    ks = di.keys()
    ks.sort()
    for k in ks:
      s += "/{}_{}".format(k, di[k])
    return s

  def _asure_path(self, path):
    if not os.path.exists(path):
      os.makedirs(path)

  def slide_pages(self):
    ps = Slider(self.root, self.name)
    return ps.pages()

  def slides_path(self, size='mid'):
    ps = Slider(self.root, self.name)
    return ps.slides_path(size)

  def store(self):
    sp = self.store_path()
    return pd.HDFStore(sp, format='t', data_columns=True,
                       complib='blosc', complevel=self.comp)

  def make_path(self, resource='stores', ext='h5', asure=True, root=False):
    """
    resource: resource name in path
    ext: file extension for resource, if None then return only path
    asure: make sure the path exist
    root: get only root path of given resource
    usage:
      # make simple log path, and make sure created
      self.make_path('log', None)
      # make path for log with log extension, and not check existence
      self.make_path('log', 'log', False)
    """
    rt = self.root
    pn = self.name
    cn = self._underscore(self.__class__.__name__)
    pth = "data/{}/{}/{}".format(rt, pn, resource)
    if root:
      return pth
    if asure:
      self._asure_path(pth)
    pth = "{}/{}".format(pth, cn)
    if ext is not None:
      pth = "{}.{}".format(pth, ext)
    return pth

  def store_path(self):
    """
    DEPRECATED, use make_path instead.
    """
    return self.make_path()

  def delete_file(self, tar=[('store', 'h5', False)]):
    for res, ext, root in tar:
      ph = self.make_path(res, ext, False, root)
      print ph
      if ext is None or root:  # for whole directory
        shutil.rmtree(ph)
      else:  # for a single file
        if os.path.isfile(ph):
          os.remove(ph)

  def save(self, key, data):
    self.log.info('save key ==> {}'.format(key))
    sp = self.make_path('stores', 'h5', asure=True, root=False)
    data.to_hdf(sp, key, mode='a', data_columns=True, format='t',
                complib='blosc', complevel=self.comp)
    self._save_key(sp, key)

  def load(self, key):
    sp = self.store_path()
    try:
      df = pd.read_hdf(sp, key, format='t')
    except KeyError, e:
      print e
      return None
    return df

  def notify(self, summary):
    if self.upass is None:
        return
    ps = self.upass
    cn = self._underscore(self.__class__.__name__)
    title = "Pyslider Job: {} <{}-{}> Finished". \
        format(cn, self.root, self.name)
    me = "speed.of.lightt@gmail.com"
    with Emailer(config=dict(uname=me, upass=ps)) as mailer:
      mailer.send(title, summary, ['speed.of.lightt@gmail.com'])
<|endoftext|>


<commit_before>##########################################################################
#
#  Copyright (c) 2008, Image Engine Design 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:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * 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.
#
#     * Neither the name of Image Engine Design nor the names of any
#       other contributors to this software 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 OWNER 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.
#
##########################################################################

import unittest
import os.path

from IECore import *

from IECoreGL import *
init( False )

class TestSelection( unittest.TestCase ) :

	def testSelect( self ) :

		r = Renderer()
		r.setOption( "gl:mode", StringData( "deferred" ) )
		r.setOption( "gl:searchPath:shader", StringData( os.path.dirname( __file__ ) + "/shaders" ) )

		r.worldBegin()

		r.concatTransform( M44f.createTranslated( V3f( 0, 0, -5 ) ) )

		r.setAttribute( "name", StringData( "one" ) )
		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 1, 0, 0 ) ) } )
		r.geometry( "sphere", {}, {} )

		r.concatTransform( M44f.createTranslated( V3f( -1, 0, 0 ) ) )
		r.setAttribute( "name", StringData( "two" ) )
		r.geometry( "sphere", {}, {} )

		r.concatTransform( M44f.createTranslated( V3f( 2, 0, 0 ) ) )
		r.setAttribute( "name", StringData( "three" ) )
		r.geometry( "sphere", {}, {} )

		r.worldEnd()

		s = r.scene()
		s.setCamera( PerspectiveCamera() )

		ss = s.select( Box2f( V2f( 0 ), V2f( 1 ) ) )
		names = [ x.name.value() for x in ss ]
		self.assertEqual( len( names ), 3 )
		self.assert_( "one" in names )
		self.assert_( "two" in names )
		self.assert_( "three" in names )

	def testRegionSelect( self ) :

		r = Renderer()
		r.setOption( "gl:mode", StringData( "deferred" ) )
		r.setOption( "gl:searchPath:shader", StringData( os.path.dirname( __file__ ) + "/shaders" ) )

		r.worldBegin()

		r.concatTransform( M44f.createTranslated( V3f( 0, 0, -5 ) ) )

		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 1, 0, 0 ) ) } )
		r.concatTransform( M44f.createTranslated( V3f( -2, -2, 0 ) ) )
		r.setAttribute( "name", StringData( "red" ) )
		r.geometry( "sphere", {}, {} )

		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 0, 1, 0 ) ) } )
		r.concatTransform( M44f.createTranslated( V3f( 0, 4, 0 ) ) )
		r.setAttribute( "name", StringData( "green" ) )
		r.geometry( "sphere", {}, {} )

		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 0, 0, 1 ) ) } )
		r.concatTransform( M44f.createTranslated( V3f( 4, 0, 0 ) ) )
		r.setAttribute( "name", StringData( "blue" ) )
		r.geometry( "sphere", {}, {} )

		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 1, 1, 1 ) ) } )
		r.concatTransform( M44f.createTranslated( V3f( 0, -4, 0 ) ) )
		r.setAttribute( "name", StringData( "white" ) )
		r.geometry( "sphere", {}, {} )

		r.worldEnd()

		s = r.scene()
		s.setCamera( PerspectiveCamera() )

		ss = s.select( Box2f( V2f( 0, 0.5 ), V2f( 0.5, 1 ) ) )
		self.assertEqual( len( ss ), 1 )
		self.assertEqual( ss[0].name.value(), "red" )

		ss = s.select( Box2f( V2f( 0 ), V2f( 0.5 ) ) )
		self.assertEqual( len( ss ), 1 )
		self.assertEqual( ss[0].name.value(), "green" )

		ss = s.select( Box2f( V2f( 0.5, 0 ), V2f( 1, 0.5 ) ) )
		self.assertEqual( len( ss ), 1 )
		self.assertEqual( ss[0].name.value(), "blue" )

		ss = s.select( Box2f( V2f( 0.5 ), V2f( 1 ) ) )
		self.assertEqual( len( ss ), 1 )
		self.assertEqual( ss[0].name.value(), "white" )

if __name__ == "__main__":
    unittest.main()
<commit_msg>Corrected translation to account for fixed camera direction.<commit_after>##########################################################################
#
#  Copyright (c) 2008, Image Engine Design 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:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * 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.
#
#     * Neither the name of Image Engine Design nor the names of any
#       other contributors to this software 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 OWNER 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.
#
##########################################################################

import unittest
import os.path

from IECore import *

from IECoreGL import *
init( False )

class TestSelection( unittest.TestCase ) :

	def testSelect( self ) :

		r = Renderer()
		r.setOption( "gl:mode", StringData( "deferred" ) )
		r.setOption( "gl:searchPath:shader", StringData( os.path.dirname( __file__ ) + "/shaders" ) )

		r.worldBegin()

		r.concatTransform( M44f.createTranslated( V3f( 0, 0, 5 ) ) )

		r.setAttribute( "name", StringData( "one" ) )
		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 1, 0, 0 ) ) } )
		r.geometry( "sphere", {}, {} )

		r.concatTransform( M44f.createTranslated( V3f( -1, 0, 0 ) ) )
		r.setAttribute( "name", StringData( "two" ) )
		r.geometry( "sphere", {}, {} )

		r.concatTransform( M44f.createTranslated( V3f( 2, 0, 0 ) ) )
		r.setAttribute( "name", StringData( "three" ) )
		r.geometry( "sphere", {}, {} )

		r.worldEnd()

		s = r.scene()
		s.setCamera( PerspectiveCamera() )

		ss = s.select( Box2f( V2f( 0 ), V2f( 1 ) ) )
		names = [ x.name.value() for x in ss ]
		self.assertEqual( len( names ), 3 )
		self.assert_( "one" in names )
		self.assert_( "two" in names )
		self.assert_( "three" in names )

	def testRegionSelect( self ) :

		r = Renderer()
		r.setOption( "gl:mode", StringData( "deferred" ) )
		r.setOption( "gl:searchPath:shader", StringData( os.path.dirname( __file__ ) + "/shaders" ) )

		r.worldBegin()

		r.concatTransform( M44f.createTranslated( V3f( 0, 0, 5 ) ) )

		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 1, 0, 0 ) ) } )
		r.concatTransform( M44f.createTranslated( V3f( -2, -2, 0 ) ) )
		r.setAttribute( "name", StringData( "red" ) )
		r.geometry( "sphere", {}, {} )

		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 0, 1, 0 ) ) } )
		r.concatTransform( M44f.createTranslated( V3f( 0, 4, 0 ) ) )
		r.setAttribute( "name", StringData( "green" ) )
		r.geometry( "sphere", {}, {} )

		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 0, 0, 1 ) ) } )
		r.concatTransform( M44f.createTranslated( V3f( 4, 0, 0 ) ) )
		r.setAttribute( "name", StringData( "blue" ) )
		r.geometry( "sphere", {}, {} )

		r.shader( "surface", "color", { "colorValue" : Color3fData( Color3f( 1, 1, 1 ) ) } )
		r.concatTransform( M44f.createTranslated( V3f( 0, -4, 0 ) ) )
		r.setAttribute( "name", StringData( "white" ) )
		r.geometry( "sphere", {}, {} )

		r.worldEnd()

		s = r.scene()
		s.setCamera( PerspectiveCamera() )

		ss = s.select( Box2f( V2f( 0, 0.5 ), V2f( 0.5, 1 ) ) )
		self.assertEqual( len( ss ), 1 )
		self.assertEqual( ss[0].name.value(), "red" )

		ss = s.select( Box2f( V2f( 0 ), V2f( 0.5 ) ) )
		self.assertEqual( len( ss ), 1 )
		self.assertEqual( ss[0].name.value(), "green" )

		ss = s.select( Box2f( V2f( 0.5, 0 ), V2f( 1, 0.5 ) ) )
		self.assertEqual( len( ss ), 1 )
		self.assertEqual( ss[0].name.value(), "blue" )

		ss = s.select( Box2f( V2f( 0.5 ), V2f( 1 ) ) )
		self.assertEqual( len( ss ), 1 )
		self.assertEqual( ss[0].name.value(), "white" )

if __name__ == "__main__":
    unittest.main()
<|endoftext|>


<commit_before>from __future__ import unicode_literals, division, absolute_import

import logging
import re
from datetime import datetime

from flexget import plugin
from flexget.event import event

try:
    from flexget.plugins.api_tvmaze import APITVMaze

    episode_airdate = APITVMaze.episode_airdate
except ImportError:
    raise plugin.DependencyError(issued_by='est_series_tvmaze', missing='api_tvmaze',
                                 message='est_series_tvmaze requires the `api_tvmaze` plugin')

log = logging.getLogger('est_series_tvmaze')

class EstimatesSeriesTVMaze(object):
    @plugin.priority(2)
    def estimate(self, entry):
        if not all(field in entry for field in ['series_name', 'series_season', 'series_episode']):
            return
        series_name = entry['series_name']
        season = entry['series_season']
        episode_number = entry['series_episode']
        year_match = re.search('\(([\d]{4})\)', series_name)  # Gets year from title if present
        if year_match:
            year_match = year_match.group(1)

        kwargs = {}
        kwargs['maze_id'] = entry.get('tvmaze_id')
        kwargs['tvdb_id'] = entry.get('tvdb_id') or entry.get('trakt_series_tvdb_id')
        kwargs['tvrage_id'] = entry.get('tvrage_id') or entry.get('trakt_series_tvrage_id')
        kwargs['show_name'] = re.sub('\(([\d]{4})\)', '', series_name).rstrip()  # Remove year from name if present
        kwargs['show_year'] = entry.get('trakt_series_year') or entry.get('year') or entry.get(
            'imdb_year') or year_match
        kwargs['show_network'] = entry.get('network') or entry.get('trakt_series_network')
        kwargs['show_country'] = entry.get('country') or entry.get('trakt_series_country')
        kwargs['show_language'] = entry.get('language')
        kwargs['series_season'] = season
        kwargs['series_episode'] = episode_number
        kwargs['series_name'] = series_name

        log.debug(
            'Searching TVMaze for airdate of {0} season {1} episode {2}'.format(series_name, season, episode_number))
        for k, v in kwargs.items():
            if v:
                log.debug('{0}: {1}'.format(k, v))
        try:
            tvmaze_show = get_show(**kwargs)
        except ShowNotFound as e:
            log.warning('Could not found show on TVMaze: {0}'.format(e))
            return
        try:
            episode = tvmaze_show[season][episode_number]
        except SeasonNotFound as e:
            log.debug('Show {0} does not appear to have a season {1}: {2}'.format(series_name, season, e))
            return
        except EpisodeNotFound as e:
            log.debug(
                'Show {0} does not appear to have a season {1} and episode {2}: {3}'.format(series_name, season,
                                                                                episode_number, e))
            return
        if not episode.airdate:
            log.debug('empty airdate received from episode, probably TBA')
            return
        airdate = datetime.strptime(episode.airdate, '%Y-%m-%d')
        log.debug('received airdate: {0}'.format(airdate))
        return airdate
            airdate = episode_airdate(**kwargs)
            log.debug('received airdate: {0}'.format(airdate))
            return airdate
        except LookupError as e:
            log.debug(e)
        return

@event('plugin.register')
def register_plugin():
    plugin.register(EstimatesSeriesTVMaze, 'est_series_tvmaze', groups=['estimate_release'], api_ver=2)
<commit_msg>Fix check for empty airdate<commit_after>from __future__ import unicode_literals, division, absolute_import

import logging
import re

from flexget import plugin
from flexget.event import event

try:
    from flexget.plugins.api_tvmaze import APITVMaze

    episode_airdate = APITVMaze.episode_airdate
except ImportError:
    raise plugin.DependencyError(issued_by='est_series_tvmaze', missing='api_tvmaze',
                                 message='est_series_tvmaze requires the `api_tvmaze` plugin')

log = logging.getLogger('est_series_tvmaze')

class EstimatesSeriesTVMaze(object):
    @plugin.priority(2)
    def estimate(self, entry):
        if not all(field in entry for field in ['series_name', 'series_season', 'series_episode']):
            return
        series_name = entry['series_name']
        season = entry['series_season']
        episode_number = entry['series_episode']
        year_match = re.search('\(([\d]{4})\)', series_name)  # Gets year from title if present
        if year_match:
            year_match = year_match.group(1)

        kwargs = {}
        kwargs['maze_id'] = entry.get('tvmaze_id')
        kwargs['tvdb_id'] = entry.get('tvdb_id') or entry.get('trakt_series_tvdb_id')
        kwargs['tvrage_id'] = entry.get('tvrage_id') or entry.get('trakt_series_tvrage_id')
        kwargs['show_name'] = re.sub('\(([\d]{4})\)', '', series_name).rstrip()  # Remove year from name if present
        kwargs['show_year'] = entry.get('trakt_series_year') or entry.get('year') or entry.get(
            'imdb_year') or year_match
        kwargs['show_network'] = entry.get('network') or entry.get('trakt_series_network')
        kwargs['show_country'] = entry.get('country') or entry.get('trakt_series_country')
        kwargs['show_language'] = entry.get('language')
        kwargs['series_season'] = season
        kwargs['series_episode'] = episode_number
        kwargs['series_name'] = series_name

        log.debug(
            'Searching TVMaze for airdate of {0} season {1} episode {2}'.format(series_name, season, episode_number))
        for k, v in kwargs.items():
            if v:
                log.debug('{0}: {1}'.format(k, v))
        try:
            airdate = episode_airdate(**kwargs)
        except LookupError as e:
            log.debug(e)
            return
        if airdate:
            log.debug('received airdate: {0}'.format(airdate))
            return airdate
        return

@event('plugin.register')
def register_plugin():
    plugin.register(EstimatesSeriesTVMaze, 'est_series_tvmaze', groups=['estimate_release'], api_ver=2)
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand, CommandError
from rois_manager.models import Slice, Core
from reviews_manager.models import ROIsAnnotationStep
from django.contrib.auth.models import User

try:
    import simplejson as json
except ImportError:
    import json

import logging, math

logger = logging.getLogger('promort_commands')

class Command(BaseCommand):
    help = """
    """

    def add_arguments(self, parser):
        parser.add_argument('--rois_file', dest='rois_file', type=str, required=True,
                            help='file containing ROIs in JSON format')
        parser.add_argument('--slide_id', dest='slide_id', type=str, required=True,
                            help='slide ID')
        parser.add_argument('--username', dest='username', type=str, required=True,
                            help='user that will create the new ROIs')
        parser.add_argument('--clear_rois', action='store_true',
                            help='delete existing ROIs for this slide\'s existing annotation steps')

    def _load_user(self, username):
        try:
            return User.objects.get(username=username)
        except User.DoesNotExist:
            logger.error('There is no user with username %s', username)
            raise CommandError('There is no user with username %s' % username)

    def _load_annotation_steps(self, slide_id):
        annotations_steps = ROIsAnnotationStep.objects.filter(
            slide_id=slide_id, start_date__isnull=True
        )
        logger.info('Loaded %d ROIs annotation steps' % len(annotations_steps))
        return annotations_steps

    def _clean_annotation_step(self, annotation_step_obj):
        for core in annotation_step_obj.cores:
            core.delete()
        for slice in annotation_step_obj.slices.all():
            slice.delete()

    def _create_roi_json(self, roi_coordinates, shape_id, stroke_color):
        return {
            'fill_color': '#ffffff',
            'fill_alpha': 0.01,
            'hidden': False,
            'segments': [{'point': {'x': rc[0], 'y': rc[1]}} for rc in roi_coordinates],
            'shape_id': shape_id,
            'stroke_alpha': 1,
            'stroke_color': stroke_color,
            'stroke_width': 40,
            'type': 'polygon'
        }

    def _create_slice(self, slice_coordinates, slice_id, annotation_step, user):
        roi_json = self._create_roi_json(slice_coordinates, 'slice_s%02d' % slice_id, '#000000')
        slice = Slice(label=roi_json['shape_id'], annotation_step=annotation_step,
                      slide=annotation_step.slide, author=user, roi_json=json.dumps(roi_json))
        slice.save()
        return slice

    def _create_core(self, core_coordinates, core_length, core_area, slice, slice_id, core_id, user):
        roi_json = self._create_roi_json(core_coordinates, 'core_s%02d_c%02d' % (slice_id, core_id),
                                         '#0000ff')
        core = Core(label=roi_json['shape_id'], slice=slice, author=user, roi_json=json.dumps(roi_json),
                    length=core_length, area=core_area)
        core.save()
        return core

    def handle(self, *args, **opts):
        logger.info('== Starting import job ==')
        annotation_steps = self._load_annotation_steps(opts['slide_id'])
        if len(annotation_steps) > 0:
            user = self._load_user(opts['username'])
            with open(opts['rois_file']) as jfile:
                rois = json.loads(jfile.read())
                for step in annotation_steps:
                    logger.info('-- Processing ROIs annotation step %s', step.label)
                    if opts['clear_rois']:
                        logger.info('Cleaning existing ROIs')
                        self._clean_annotation_step(step)
                    for slice_index, slice in enumerate(rois):
                        logger.info('- Loading slice %d of %d', slice_index+1, len(rois))
                        slide_mpp = step.slide.image_microns_per_pixel
                        slice_obj = self._create_slice(slice['coordinates'], slice_index+1, step, user)
                        logger.info('Slice saved with ID %d', slice_obj.id)
                        for core_index, core in enumerate(slice['cores']):
                            logger.info('Loading core %d of %d', core_index+1, len(slice['cores']))
                            core_obj = self._create_core(core['coordinates'], core['length'] * slide_mpp,
                                                         core['area'] * math.pow(slide_mpp, 2),
                                                         slice_obj, slice_index+1, core_index+1, user)
                            logger.info('Core saved with ID %d', core_obj.id)
        else:
            logger.info('== There are no suitable ROIs annotation steps')
        logger.info('== Job completed ==')
<commit_msg>added proper cores count when creating a slice<commit_after>from django.core.management.base import BaseCommand, CommandError
from rois_manager.models import Slice, Core
from reviews_manager.models import ROIsAnnotationStep
from django.contrib.auth.models import User

try:
    import simplejson as json
except ImportError:
    import json

import logging, math

logger = logging.getLogger('promort_commands')

class Command(BaseCommand):
    help = """
    """

    def add_arguments(self, parser):
        parser.add_argument('--rois_file', dest='rois_file', type=str, required=True,
                            help='file containing ROIs in JSON format')
        parser.add_argument('--slide_id', dest='slide_id', type=str, required=True,
                            help='slide ID')
        parser.add_argument('--username', dest='username', type=str, required=True,
                            help='user that will create the new ROIs')
        parser.add_argument('--clear_rois', action='store_true',
                            help='delete existing ROIs for this slide\'s existing annotation steps')

    def _load_user(self, username):
        try:
            return User.objects.get(username=username)
        except User.DoesNotExist:
            logger.error('There is no user with username %s', username)
            raise CommandError('There is no user with username %s' % username)

    def _load_annotation_steps(self, slide_id):
        annotations_steps = ROIsAnnotationStep.objects.filter(
            slide_id=slide_id, start_date__isnull=True
        )
        logger.info('Loaded %d ROIs annotation steps' % len(annotations_steps))
        return annotations_steps

    def _clean_annotation_step(self, annotation_step_obj):
        for core in annotation_step_obj.cores:
            core.delete()
        for slice in annotation_step_obj.slices.all():
            slice.delete()

    def _create_roi_json(self, roi_coordinates, shape_id, stroke_color):
        return {
            'fill_color': '#ffffff',
            'fill_alpha': 0.01,
            'hidden': False,
            'segments': [{'point': {'x': rc[0], 'y': rc[1]}} for rc in roi_coordinates],
            'shape_id': shape_id,
            'stroke_alpha': 1,
            'stroke_color': stroke_color,
            'stroke_width': 40,
            'type': 'polygon'
        }

    def _create_slice(self, slice_coordinates, slice_id, cores_count, annotation_step, user):
        roi_json = self._create_roi_json(slice_coordinates, 'slice_s%02d' % slice_id, '#000000')
        slice = Slice(label=roi_json['shape_id'], annotation_step=annotation_step,
                      slide=annotation_step.slide, author=user, roi_json=json.dumps(roi_json),
                      total_cores=cores_count)
        slice.save()
        return slice

    def _create_core(self, core_coordinates, core_length, core_area, slice, slice_id, core_id, user):
        roi_json = self._create_roi_json(core_coordinates, 'core_s%02d_c%02d' % (slice_id, core_id),
                                         '#0000ff')
        core = Core(label=roi_json['shape_id'], slice=slice, author=user, roi_json=json.dumps(roi_json),
                    length=core_length, area=core_area)
        core.save()
        return core

    def handle(self, *args, **opts):
        logger.info('== Starting import job ==')
        annotation_steps = self._load_annotation_steps(opts['slide_id'])
        if len(annotation_steps) > 0:
            user = self._load_user(opts['username'])
            with open(opts['rois_file']) as jfile:
                rois = json.loads(jfile.read())
                for step in annotation_steps:
                    logger.info('-- Processing ROIs annotation step %s', step.label)
                    if opts['clear_rois']:
                        logger.info('Cleaning existing ROIs')
                        self._clean_annotation_step(step)
                    for slice_index, slice in enumerate(rois):
                        logger.info('- Loading slice %d of %d', slice_index+1, len(rois))
                        slide_mpp = step.slide.image_microns_per_pixel
                        slice_obj = self._create_slice(slice['coordinates'], slice_index+1, len(slice['cores']),
                                                       step, user)
                        logger.info('Slice saved with ID %d', slice_obj.id)
                        for core_index, core in enumerate(slice['cores']):
                            logger.info('Loading core %d of %d', core_index+1, len(slice['cores']))
                            core_obj = self._create_core(core['coordinates'], core['length'] * slide_mpp,
                                                         core['area'] * math.pow(slide_mpp, 2),
                                                         slice_obj, slice_index+1, core_index+1, user)
                            logger.info('Core saved with ID %d', core_obj.id)
        else:
            logger.info('== There are no suitable ROIs annotation steps')
        logger.info('== Job completed ==')
<|endoftext|>


<commit_before>################################################################################
#  Licensed to the Apache Software Foundation (ASF) 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 os
import tempfile

from pyflink.table import TableEnvironment, TableConfig
from pyflink.table.table_sink import CsvTableSink
from pyflink.table.table_source import CsvTableSource
from pyflink.table.types import DataTypes

# TODO: the word_count.py is just a test example for CLI.
#  After pyflink have aligned Java Table API Connectors, this example will be improved.
def word_count():
    tmp_dir = tempfile.gettempdir()
    source_path = tmp_dir + '/streaming.csv'
    if os.path.isfile(source_path):
        os.remove(source_path)
    content = "line Licensed to the Apache Software Foundation ASF under one " \
              "line or more contributor license agreements See the NOTICE file " \
              "line distributed with this work for additional information " \
              "line regarding copyright ownership The ASF licenses this file " \
              "to you under the Apache License Version the " \
              "License you may not use this file except in compliance " \
              "with the License"

    with open(source_path, 'w') as f:
        for word in content.split(" "):
            f.write(",".join([word, "1"]))
            f.write("\n")
            f.flush()
        f.close()

    t_config = TableConfig.Builder().as_batch_execution().build()
    t_env = TableEnvironment.create(t_config)

    field_names = ["word", "cout"]
    field_types = [DataTypes.STRING(), DataTypes.BIGINT()]

    # register Orders table in table environment
    t_env.register_table_source(
        "Word",
        CsvTableSource(source_path, field_names, field_types))

    # register Results table in table environment
    tmp_dir = tempfile.gettempdir()
    tmp_csv = tmp_dir + '/streaming2.csv'
    if os.path.isfile(tmp_csv):
        os.remove(tmp_csv)

    t_env.register_table_sink(
        "Results",
        field_names, field_types, CsvTableSink(tmp_csv))

    t_env.scan("Word") \
        .group_by("word") \
        .select("word, count(1) as count") \
        .insert_into("Results")

    t_env.execute()

if __name__ == '__main__':
    word_count()
<commit_msg>[FLINK-12757][python] Improves the word_count example to use the descriptor API<commit_after>################################################################################
#  Licensed to the Apache Software Foundation (ASF) 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 logging
import os
import shutil
import sys
import tempfile

from pyflink.table import TableEnvironment, TableConfig, FileSystem, OldCsv, Schema
from pyflink.table.types import DataTypes

def word_count():
    content = "line Licensed to the Apache Software Foundation ASF under one " \
              "line or more contributor license agreements See the NOTICE file " \
              "line distributed with this work for additional information " \
              "line regarding copyright ownership The ASF licenses this file " \
              "to you under the Apache License Version the " \
              "License you may not use this file except in compliance " \
              "with the License"

    t_config = TableConfig.Builder().as_batch_execution().build()
    t_env = TableEnvironment.create(t_config)

    # register Results table in table environment
    tmp_dir = tempfile.gettempdir()
    result_path = tmp_dir + '/result'
    if os.path.exists(result_path):
        try:
            if os.path.isfile(result_path):
                os.remove(result_path)
            else:
                shutil.rmtree(result_path)
        except OSError as e:
            logging.error("Error removing directory: %s - %s.", e.filename, e.strerror)

    logging.info("Results directory: %s", result_path)

    t_env.connect(FileSystem().path(result_path)) \
        .with_format(OldCsv()
                     .field_delimiter(',')
                     .field("word", DataTypes.STRING())
                     .field("count", DataTypes.BIGINT())) \
        .with_schema(Schema()
                     .field("word", DataTypes.STRING())
                     .field("count", DataTypes.BIGINT())) \
        .register_table_sink("Results")

    elements = [(word, 1) for word in content.split(" ")]
    t_env.from_elements(elements, ["word", "count"]) \
         .group_by("word") \
         .select("word, count(1) as count") \
         .insert_into("Results")

    t_env.execute()

if __name__ == '__main__':
    logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")

    word_count()
<|endoftext|>


<commit_before>"""
Key bindings which are also known by GNU readline by the given names.

See: http://www.delorie.com/gnu/docs/readline/rlman_13.html
"""
from __future__ import unicode_literals
from prompt_toolkit.enums import IncrementalSearchDirection, SEARCH_BUFFER
from six.moves import range
import six

from .completion import generate_completions

__all__ = (
    'get_by_name',
)

# Registry that maps the Readline command names to their handlers.
_readline_commands = {}

def register(name):
    """
    Store handler in the `_readline_commands` dictionary.
    """
    assert isinstance(name, six.text_type)
    def decorator(handler):
        assert callable(handler)

        _readline_commands[name] = handler
        return handler
    return decorator

def get_by_name(name):
    """
    Return the handler for the (Readline) command with the given name.
    """
    try:
        return _readline_commands[name]
    except KeyError:
        raise KeyError('Unknown readline command: %r' % name)

#
# Commands for moving
# See: http://www.delorie.com/gnu/docs/readline/rlman_14.html
#

@register('beginning-of-line')
def beginning_of_line(event):
    " Move to the start of the current line. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_start_of_line_position(after_whitespace=False)

@register('end-of-line')
def end_of_line(event):
    " Move to the end of the line. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_end_of_line_position()

@register('forward-char')
def forward_char(event):
    " Move forward a character. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_cursor_right_position(count=event.arg)

@register('backward-char')
def backward_char(event):
    " Move back a character. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_cursor_left_position(count=event.arg)

@register('forward-word')
def forward_word(event):
    """
    Move forward to the end of the next word. Words are composed of letters and
    digits.
    """
    buff = event.current_buffer
    pos = buff.document.find_next_word_ending(count=event.arg)

    if pos:
        buff.cursor_position += pos

@register('backward-word')
def backward_word(event):
    """
    Move back to the start of the current or previous word. Words are composed
    of letters and digits.
    """
    buff = event.current_buffer
    pos = buff.document.find_previous_word_beginning(count=event.arg)

    if pos:
        buff.cursor_position += pos

@register('clear-screen')
def clear_screen(event):
    """
    Clear the screen and redraw everything at the top of the screen.
    """
    event.cli.renderer.clear()

@register('redraw-current-line')
def redraw_current_line(event):
    """
    Refresh the current line.
    (Readline defines this command, but prompt-toolkit doesn't have it.)
    """
    pass

#
# Commands for manipulating the history.
# See: http://www.delorie.com/gnu/docs/readline/rlman_15.html
#

@register('accept-line')
def accept_line(event):
    " Accept the line regardless of where the cursor is. "
    b = event.current_buffer
    b.accept_action.validate_and_handle(event.cli, b)

@register('previous-history')
def previous_history(event):
    " Move `back' through the history list, fetching the previous command.  "
    event.current_buffer.history_backward(count=event.count)

@register('next-history')
def next_history(event):
    " Move `forward' through the history list, fetching the next command. "
    event.current_buffer.history_forward(count=event.count)

@register('beginning-of-history')
def beginning_of_history(event):
    " Move to the first line in the history. "
    event.current_buffer.go_to_history(0)

@register('end-of-history')
def end_of_history(event):
    """
    Move to the end of the input history, i.e., the line currently being entered.
    """
    event.current_buffer.history_forward(count=10**100)
    buff = event.current_buffer
    buff.go_to_history(len(buff._working_lines) - 1)

@register('reverse-search-history')
def reverse_search_history(event):
    """
    Search backward starting at the current line and moving `up' through
    the history as necessary. This is an incremental search.
    """
    event.cli.current_search_state.direction = IncrementalSearchDirection.BACKWARD
    event.cli.push_focus(SEARCH_BUFFER)

#
# Commands for changing text
#

@register('end-of-file')
def end_of_file(event):
    """
    Exit.
    """
    event.cli.exit()

@register('delete-char')
def delete_char(event):
    " Delete character before the cursor. "
    deleted = event.current_buffer.delete(count=event.arg)
    if not deleted:
        event.cli.output.bell()

@register('backward-delete-char')
def backward_delete_char(event):
    " Delete the character behind the cursor. "
    deleted = event.current_buffer.delete_before_cursor(count=event.arg)
    if not deleted:
        event.cli.output.bell()

@register('self-insert')
def self_insert(event):
    " Insert yourself. "
    event.current_buffer.insert_text(event.data * event.arg)

@register('transpose-chars')
def transpose_chars(event):
    """
    Emulate Emacs transpose-char behavior: at the beginning of the buffer,
    do nothing.  At the end of a line or buffer, swap the characters before
    the cursor.  Otherwise, move the cursor right, and then swap the
    characters before the cursor.
    """
    b = event.current_buffer
    p = b.cursor_position
    if p == 0:
        return
    elif p == len(b.text) or b.text[p] == '\n':
        b.swap_characters_before_cursor()
    else:
        b.cursor_position += b.document.get_cursor_right_position()
        b.swap_characters_before_cursor()

@register('uppercase-word')
def uppercase_word(event):
    """
    Uppercase the current (or following) word.
    """
    buff = event.current_buffer

    for i in range(event.arg):
        pos = buff.document.find_next_word_ending()
        words = buff.document.text_after_cursor[:pos]
        buff.insert_text(words.upper(), overwrite=True)

@register('downcase-word')
def downcase_word(event):
    """
    Lowercase the current (or following) word.
    """
    buff = event.current_buffer

    for i in range(event.arg):  # XXX: not DRY: see meta_c and meta_u!!
        pos = buff.document.find_next_word_ending()
        words = buff.document.text_after_cursor[:pos]
        buff.insert_text(words.lower(), overwrite=True)

@register('capitalize-word')
def capitalize_word(event):
    """
    Capitalize the current (or following) word.
    """
    buff = event.current_buffer

    for i in range(event.arg):
        pos = buff.document.find_next_word_ending()
        words = buff.document.text_after_cursor[:pos]
        buff.insert_text(words.title(), overwrite=True)

#
# Killing and yanking.
#

@register('kill-line')
def kill_line(event):
    """
    Kill the text from the cursor to the end of the line.
    """
    buff = event.current_buffer
    deleted = buff.delete(count=buff.document.get_end_of_line_position())
    event.cli.clipboard.set_text(deleted)

@register('kill-word')
def kill_word(event):
    """
    Kill from point to the end of the current word, or if between words, to the
    end of the next word. Word boundaries are the same as forward-word.
    """
    buff = event.current_buffer
    pos = buff.document.find_next_word_ending(count=event.arg)

    if pos:
        deleted = buff.delete(count=pos)
        event.cli.clipboard.set_text(deleted)

@register('unix-word-rubout')
@register('backward-kill-word')  # XXX: backward-kill-word is actually slightly different.
def unix_word_rubout(event):
    """
    Kill the word behind point. Word boundaries are the same as backward-word.
    """
    buff = event.current_buffer
    pos = buff.document.find_start_of_previous_word(count=event.arg)

    if pos is None:
        # Nothing found? delete until the start of the document.  (The
        # input starts with whitespace and no words were found before the
        # cursor.)
        pos = - buff.cursor_position

    if pos:
        deleted = buff.delete_before_cursor(count=-pos)

        # If the previous key press was also Control-W, concatenate deleted
        # text.
        if event.is_repeat:
            deleted += event.cli.clipboard.get_data().text

        event.cli.clipboard.set_text(deleted)
    else:
        # Nothing to delete. Bell.
        event.cli.output.bell()

@register('delete-horizontal-space')
def delete_horizontal_space(event):
    " Delete all spaces and tabs around point. "
    buff = event.current_buffer
    text_before_cursor = buff.document.text_before_cursor
    text_after_cursor = buff.document.text_after_cursor

    delete_before = len(text_before_cursor) - len(text_before_cursor.rstrip('\t '))
    delete_after = len(text_after_cursor) - len(text_after_cursor.lstrip('\t '))

    buff.delete_before_cursor(count=delete_before)
    buff.delete(count=delete_after)

@register('unix-line-discard')
def unix_line_discard(event):
    """
    Kill backward from the cursor to the beginning of the current line.
    """
    buff = event.current_buffer
    deleted = buff.delete_before_cursor(count=-buff.document.get_start_of_line_position())
    event.cli.clipboard.set_text(deleted)

@register('yank')
@register('yank-pop')
def yank(event):
    """
    Paste before cursor.
    """
    event.current_buffer.paste_clipboard_data(
        event.cli.clipboard.get_data(), count=event.arg, before=True)

#
# Completion.
#

@register('complete')
def complete(event):
    generate_completions(event)

#
# Miscellaneous Commands.
#

@register('undo')
def undo(event):
    " Incremental undo. "
    event.current_buffer.undo()
<commit_msg>Delete in front of the cursor when a negative argument has been given to backspace.<commit_after>"""
Key bindings which are also known by GNU readline by the given names.

See: http://www.delorie.com/gnu/docs/readline/rlman_13.html
"""
from __future__ import unicode_literals
from prompt_toolkit.enums import IncrementalSearchDirection, SEARCH_BUFFER
from six.moves import range
import six

from .completion import generate_completions

__all__ = (
    'get_by_name',
)

# Registry that maps the Readline command names to their handlers.
_readline_commands = {}

def register(name):
    """
    Store handler in the `_readline_commands` dictionary.
    """
    assert isinstance(name, six.text_type)
    def decorator(handler):
        assert callable(handler)

        _readline_commands[name] = handler
        return handler
    return decorator

def get_by_name(name):
    """
    Return the handler for the (Readline) command with the given name.
    """
    try:
        return _readline_commands[name]
    except KeyError:
        raise KeyError('Unknown readline command: %r' % name)

#
# Commands for moving
# See: http://www.delorie.com/gnu/docs/readline/rlman_14.html
#

@register('beginning-of-line')
def beginning_of_line(event):
    " Move to the start of the current line. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_start_of_line_position(after_whitespace=False)

@register('end-of-line')
def end_of_line(event):
    " Move to the end of the line. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_end_of_line_position()

@register('forward-char')
def forward_char(event):
    " Move forward a character. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_cursor_right_position(count=event.arg)

@register('backward-char')
def backward_char(event):
    " Move back a character. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_cursor_left_position(count=event.arg)

@register('forward-word')
def forward_word(event):
    """
    Move forward to the end of the next word. Words are composed of letters and
    digits.
    """
    buff = event.current_buffer
    pos = buff.document.find_next_word_ending(count=event.arg)

    if pos:
        buff.cursor_position += pos

@register('backward-word')
def backward_word(event):
    """
    Move back to the start of the current or previous word. Words are composed
    of letters and digits.
    """
    buff = event.current_buffer
    pos = buff.document.find_previous_word_beginning(count=event.arg)

    if pos:
        buff.cursor_position += pos

@register('clear-screen')
def clear_screen(event):
    """
    Clear the screen and redraw everything at the top of the screen.
    """
    event.cli.renderer.clear()

@register('redraw-current-line')
def redraw_current_line(event):
    """
    Refresh the current line.
    (Readline defines this command, but prompt-toolkit doesn't have it.)
    """
    pass

#
# Commands for manipulating the history.
# See: http://www.delorie.com/gnu/docs/readline/rlman_15.html
#

@register('accept-line')
def accept_line(event):
    " Accept the line regardless of where the cursor is. "
    b = event.current_buffer
    b.accept_action.validate_and_handle(event.cli, b)

@register('previous-history')
def previous_history(event):
    " Move `back' through the history list, fetching the previous command.  "
    event.current_buffer.history_backward(count=event.count)

@register('next-history')
def next_history(event):
    " Move `forward' through the history list, fetching the next command. "
    event.current_buffer.history_forward(count=event.count)

@register('beginning-of-history')
def beginning_of_history(event):
    " Move to the first line in the history. "
    event.current_buffer.go_to_history(0)

@register('end-of-history')
def end_of_history(event):
    """
    Move to the end of the input history, i.e., the line currently being entered.
    """
    event.current_buffer.history_forward(count=10**100)
    buff = event.current_buffer
    buff.go_to_history(len(buff._working_lines) - 1)

@register('reverse-search-history')
def reverse_search_history(event):
    """
    Search backward starting at the current line and moving `up' through
    the history as necessary. This is an incremental search.
    """
    event.cli.current_search_state.direction = IncrementalSearchDirection.BACKWARD
    event.cli.push_focus(SEARCH_BUFFER)

#
# Commands for changing text
#

@register('end-of-file')
def end_of_file(event):
    """
    Exit.
    """
    event.cli.exit()

@register('delete-char')
def delete_char(event):
    " Delete character before the cursor. "
    deleted = event.current_buffer.delete(count=event.arg)
    if not deleted:
        event.cli.output.bell()

@register('backward-delete-char')
def backward_delete_char(event):
    " Delete the character behind the cursor. "
    if event.arg < 0:
        # When a negative argument has been given, this should delete in front
        # of the cursor.
        deleted = event.current_buffer.delete(count=-event.arg)
    else:
        deleted = event.current_buffer.delete_before_cursor(count=event.arg)

    if not deleted:
        event.cli.output.bell()

@register('self-insert')
def self_insert(event):
    " Insert yourself. "
    event.current_buffer.insert_text(event.data * event.arg)

@register('transpose-chars')
def transpose_chars(event):
    """
    Emulate Emacs transpose-char behavior: at the beginning of the buffer,
    do nothing.  At the end of a line or buffer, swap the characters before
    the cursor.  Otherwise, move the cursor right, and then swap the
    characters before the cursor.
    """
    b = event.current_buffer
    p = b.cursor_position
    if p == 0:
        return
    elif p == len(b.text) or b.text[p] == '\n':
        b.swap_characters_before_cursor()
    else:
        b.cursor_position += b.document.get_cursor_right_position()
        b.swap_characters_before_cursor()

@register('uppercase-word')
def uppercase_word(event):
    """
    Uppercase the current (or following) word.
    """
    buff = event.current_buffer

    for i in range(event.arg):
        pos = buff.document.find_next_word_ending()
        words = buff.document.text_after_cursor[:pos]
        buff.insert_text(words.upper(), overwrite=True)

@register('downcase-word')
def downcase_word(event):
    """
    Lowercase the current (or following) word.
    """
    buff = event.current_buffer

    for i in range(event.arg):  # XXX: not DRY: see meta_c and meta_u!!
        pos = buff.document.find_next_word_ending()
        words = buff.document.text_after_cursor[:pos]
        buff.insert_text(words.lower(), overwrite=True)

@register('capitalize-word')
def capitalize_word(event):
    """
    Capitalize the current (or following) word.
    """
    buff = event.current_buffer

    for i in range(event.arg):
        pos = buff.document.find_next_word_ending()
        words = buff.document.text_after_cursor[:pos]
        buff.insert_text(words.title(), overwrite=True)

#
# Killing and yanking.
#

@register('kill-line')
def kill_line(event):
    """
    Kill the text from the cursor to the end of the line.
    """
    buff = event.current_buffer
    deleted = buff.delete(count=buff.document.get_end_of_line_position())
    event.cli.clipboard.set_text(deleted)

@register('kill-word')
def kill_word(event):
    """
    Kill from point to the end of the current word, or if between words, to the
    end of the next word. Word boundaries are the same as forward-word.
    """
    buff = event.current_buffer
    pos = buff.document.find_next_word_ending(count=event.arg)

    if pos:
        deleted = buff.delete(count=pos)
        event.cli.clipboard.set_text(deleted)

@register('unix-word-rubout')
@register('backward-kill-word')  # XXX: backward-kill-word is actually slightly different.
def unix_word_rubout(event):
    """
    Kill the word behind point. Word boundaries are the same as backward-word.
    """
    buff = event.current_buffer
    pos = buff.document.find_start_of_previous_word(count=event.arg)

    if pos is None:
        # Nothing found? delete until the start of the document.  (The
        # input starts with whitespace and no words were found before the
        # cursor.)
        pos = - buff.cursor_position

    if pos:
        deleted = buff.delete_before_cursor(count=-pos)

        # If the previous key press was also Control-W, concatenate deleted
        # text.
        if event.is_repeat:
            deleted += event.cli.clipboard.get_data().text

        event.cli.clipboard.set_text(deleted)
    else:
        # Nothing to delete. Bell.
        event.cli.output.bell()

@register('delete-horizontal-space')
def delete_horizontal_space(event):
    " Delete all spaces and tabs around point. "
    buff = event.current_buffer
    text_before_cursor = buff.document.text_before_cursor
    text_after_cursor = buff.document.text_after_cursor

    delete_before = len(text_before_cursor) - len(text_before_cursor.rstrip('\t '))
    delete_after = len(text_after_cursor) - len(text_after_cursor.lstrip('\t '))

    buff.delete_before_cursor(count=delete_before)
    buff.delete(count=delete_after)

@register('unix-line-discard')
def unix_line_discard(event):
    """
    Kill backward from the cursor to the beginning of the current line.
    """
    buff = event.current_buffer
    deleted = buff.delete_before_cursor(count=-buff.document.get_start_of_line_position())
    event.cli.clipboard.set_text(deleted)

@register('yank')
@register('yank-pop')
def yank(event):
    """
    Paste before cursor.
    """
    event.current_buffer.paste_clipboard_data(
        event.cli.clipboard.get_data(), count=event.arg, before=True)

#
# Completion.
#

@register('complete')
def complete(event):
    generate_completions(event)

#
# Miscellaneous Commands.
#

@register('undo')
def undo(event):
    " Incremental undo. "
    event.current_buffer.undo()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# coding: utf-8

# ==================================================
# open
# --------------------------------------------------

from __future__ import print_function
from __future__ import absolute_import
import os
import tct
import sys

VERSION = '2.7.0'

params = tct.readjson(sys.argv[1])
binabspath = sys.argv[2]
facts = tct.readjson(params['factsfile'])
milestones = tct.readjson(params['milestonesfile'])
resultfile = params['resultfile']
result = tct.readjson(resultfile)
loglist = result['loglist'] = result.get('loglist', [])
toolname = params['toolname']
toolname_pure = params['toolname_pure']
workdir = params['workdir']
exitcode = CONTINUE = 0

# ==================================================
# Make a copy of milestones for later inspection?
# --------------------------------------------------

if 0 or milestones.get('debug_always_make_milestones_snapshot'):
    tct.make_snapshot_of_milestones(params['milestonesfile'], sys.argv[1])

# ==================================================
# Helper functions
# --------------------------------------------------

def lookup(D, *keys, **kwdargs):
    result = tct.deepget(D, *keys, **kwdargs)
    loglist.append((keys, result))
    return result

# ==================================================
# define
# --------------------------------------------------

lockfiles_removed = []
removed_dirs = []
toolchain_actions = lookup(params, 'toolchain_actions', default=[])
xeq_name_cnt = 0

# ==================================================
# Check params
# --------------------------------------------------

if exitcode == CONTINUE:
    loglist.append('CHECK PARAMS')

    lockfile_name = lookup(milestones, 'lockfile_name')
    run_id = lookup(facts, 'run_id')
    toolchain_temp_home = lookup(params, 'toolchain_temp_home')
    if not (lockfile_name and run_id and toolchain_temp_home):
        exitcode = 22

if exitcode == CONTINUE:
    loglist.append('PARAMS are ok')
else:
    loglist.append('PROBLEM with required params')

# =========================================================
# Prepare for a subprocess with perfect logging
# ---------------------------------------------------------

if exitcode == CONTINUE:

    import codecs
    import os
    import subprocess

    def cmdline(cmd, cwd=None):
        if cwd is None:
            cwd = os.getcwd()
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd)
        out, err = process.communicate()
        exitcode = process.returncode
        return exitcode, cmd, out, err

    def execute_cmdlist(cmdlist):
        global xeq_name_cnt
        cmd = ' '.join(cmdlist)
        cmd_multiline = ' \\\n   '.join(cmdlist) + '\n'
        exitcode, cmd, out, err = cmdline(cmd, cwd=workdir)
        loglist.append({'exitcode': exitcode, 'cmd': cmd, 'out': out, 'err': err})
        xeq_name_cnt += 1
        filename_cmd = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'cmd')
        filename_err = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'err')
        filename_out = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'out')

        with codecs.open(os.path.join(workdir, filename_cmd), 'w', 'utf-8') as f2:
            f2.write(cmd_multiline.decode('utf-8', 'replace'))

        with codecs.open(os.path.join(workdir, filename_out), 'w', 'utf-8') as f2:
            f2.write(out.decode('utf-8', 'replace'))

        with codecs.open(os.path.join(workdir, filename_err), 'w', 'utf-8') as f2:
            f2.write(err.decode('utf-8', 'replace'))

        return exitcode, cmd, out, err

    # exitcode, cmd, out, err = execute_cmdlist(cmdlist)

# ==================================================
# work
# --------------------------------------------------

if 0 and exitcode == CONTINUE and 'This clause is experimental research':
    import os

    cmdlist = ['find /proc',
               '-maxdepth 1',
               '-user marble',
               '-type d',
               # '-mmin +$AGE',
               ]
    exitcode_temp, cmd, out, err = execute_cmdlist(cmdlist)

    for one in out.split('\n'):
        one = one.strip().strip('/proc/')
        pid = None
        try:
            pid = int(one)
        except ValueError:
            pass
        if one is not None:
            exitcode_temp, cmd, out, err = execute_cmdlist(['ps', str(one)])

if exitcode == CONTINUE:
    import shutil

if 'help' in toolchain_actions and exitcode == CONTINUE:
    'Should not occur'

if 'version' in toolchain_actions and exitcode == CONTINUE:
    print(VERSION)
    exitcode = 90

if 'unlock' in toolchain_actions and exitcode == CONTINUE:
    for top, dirs, files in os.walk(toolchain_temp_home):
        dirs[:] = [] # stop recursion
        for fname in files:
            if fname == lockfile_name:
                lockfile = os.path.join(top, fname)
                os.remove(lockfile)
                lockfiles_removed.append(lockfile)
    exitcode = 90

if 'clean' in toolchain_actions and exitcode == CONTINUE:
    for top, dirs, files in os.walk(toolchain_temp_home):
        dirs.sort()
        for adir in dirs:
            fpath = os.path.join(top, adir)
            if not run_id in adir:
                if os.path.isdir(fpath):
                    shutil.rmtree(fpath)
        dirs[:] = [] # stop recursion
    exitcode = 90

# ==================================================
# Set MILESTONE
# --------------------------------------------------

if exitcode == 90:
    result['MILESTONES'].append({'FINAL_EXITCODE': 0})

if lockfiles_removed:
    result['MILESTONES'].append({'lockfiles_removed': lockfiles_removed})

# ==================================================
# save result
# --------------------------------------------------

tct.writejson(result, resultfile)

# ==================================================
# Return with proper exitcode
# --------------------------------------------------

sys.exit(exitcode)
<commit_msg>Improve code of run_03-Run-action-and-exit.py<commit_after>#!/usr/bin/env python
# coding: utf-8

# ==================================================
# open
# --------------------------------------------------

from __future__ import print_function
from __future__ import absolute_import

import os
import shutil
import sys
import tct

VERSION = '2.7.0'

params = tct.readjson(sys.argv[1])
binabspath = sys.argv[2]
facts = tct.readjson(params['factsfile'])
milestones = tct.readjson(params['milestonesfile'])
resultfile = params['resultfile']
result = tct.readjson(resultfile)
loglist = result['loglist'] = result.get('loglist', [])
toolname = params['toolname']
toolname_pure = params['toolname_pure']
workdir = params['workdir']
exitcode = CONTINUE = 0

# ==================================================
# Make a copy of milestones for later inspection?
# --------------------------------------------------

if 0 or milestones.get('debug_always_make_milestones_snapshot'):
    tct.make_snapshot_of_milestones(params['milestonesfile'], sys.argv[1])

# ==================================================
# Helper functions
# --------------------------------------------------

def lookup(D, *keys, **kwdargs):
    result = tct.deepget(D, *keys, **kwdargs)
    loglist.append((keys, result))
    return result

# ==================================================
# define
# --------------------------------------------------

lockfiles_removed = []
removed_dirs = []
toolchain_actions = lookup(params, 'toolchain_actions', default=[])
xeq_name_cnt = 0

# ==================================================
# Check params
# --------------------------------------------------

if exitcode == CONTINUE:
    loglist.append('CHECK PARAMS')

    lockfile_name = lookup(milestones, 'lockfile_name')
    run_id = lookup(facts, 'run_id')
    toolchain_temp_home = lookup(params, 'toolchain_temp_home')
    if not (lockfile_name and run_id and toolchain_temp_home):
        exitcode = 22

if exitcode == CONTINUE:
    loglist.append('PARAMS are ok')
else:
    loglist.append('PROBLEM with required params')

# =========================================================
# Prepare for a subprocess with perfect logging
# ---------------------------------------------------------

if exitcode == CONTINUE:

    import codecs
    import os
    import subprocess

    def cmdline(cmd, cwd=None):
        if cwd is None:
            cwd = os.getcwd()
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd)
        out, err = process.communicate()
        exitcode = process.returncode
        return exitcode, cmd, out, err

    def execute_cmdlist(cmdlist):
        global xeq_name_cnt
        cmd = ' '.join(cmdlist)
        cmd_multiline = ' \\\n   '.join(cmdlist) + '\n'
        exitcode, cmd, out, err = cmdline(cmd, cwd=workdir)
        loglist.append({'exitcode': exitcode, 'cmd': cmd, 'out': out, 'err': err})
        xeq_name_cnt += 1
        filename_cmd = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'cmd')
        filename_err = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'err')
        filename_out = 'xeq-%s-%d-%s.txt' % (toolname_pure, xeq_name_cnt, 'out')

        with codecs.open(os.path.join(workdir, filename_cmd), 'w', 'utf-8') as f2:
            f2.write(cmd_multiline.decode('utf-8', 'replace'))

        with codecs.open(os.path.join(workdir, filename_out), 'w', 'utf-8') as f2:
            f2.write(out.decode('utf-8', 'replace'))

        with codecs.open(os.path.join(workdir, filename_err), 'w', 'utf-8') as f2:
            f2.write(err.decode('utf-8', 'replace'))

        return exitcode, cmd, out, err

    # exitcode, cmd, out, err = execute_cmdlist(cmdlist)

# ==================================================
# work
# --------------------------------------------------

if exitcode == CONTINUE:
    if 'version' in toolchain_actions:
        print(VERSION)
        exitcode = 90

if exitcode == CONTINUE:
    if 'unlock' in toolchain_actions:
        for top, dirs, files in os.walk(toolchain_temp_home):
            dirs[:] = [] # stop recursion
            for fname in files:
                if fname == lockfile_name:
                    lockfile = os.path.join(top, fname)
                    os.remove(lockfile)
                    lockfiles_removed.append(lockfile)
        exitcode = 90

if exitcode == CONTINUE:
    if 'clean' in toolchain_actions:
        for top, dirs, files in os.walk(toolchain_temp_home):
            dirs.sort()
            for adir in dirs:
                fpath = os.path.join(top, adir)
                if not run_id in adir:
                    if os.path.isdir(fpath):
                        shutil.rmtree(fpath)
            dirs[:] = [] # stop recursion
        exitcode = 90

# ==================================================
# Set MILESTONE
# --------------------------------------------------

if exitcode == 90:
    result['MILESTONES'].append({'FINAL_EXITCODE': 0})

if lockfiles_removed:
    result['MILESTONES'].append({'lockfiles_removed': lockfiles_removed})

# ==================================================
# save result
# --------------------------------------------------

tct.writejson(result, resultfile)

# ==================================================
# Return with proper exitcode
# --------------------------------------------------

sys.exit(exitcode)
<|endoftext|>


<commit_before>from ddt import data
from django.core.exceptions import ObjectDoesNotExist

from . import factories
from .. import models

class cached_class_property(object):
    """ Allows to reach classmethod as property and cache returned value in class """

    def __init__(self, method):
        self.method = method
        self.cache_name = self.method.__name__ + '_cached'

    def __get__(self, owner_self, owner_cls):
        try:
            value = getattr(owner_cls, self.cache_name)
            value.refresh_from_db()
        except (AttributeError, ObjectDoesNotExist):
            value = self.method(owner_cls)
            setattr(owner_cls, self.cache_name, value)
        return value

class test_data(object):
    """ Container with standard structure objects that can be used for testing.

        Use test_data as enum:

            def test_admin_can_reach_project(self):
                self.client.force_authenticate(user=test_data.admin)
                url = factories.ProjectFactory.get_url(test_data.project)
                response = self.client.get(url)
                self.assertEqual(response.status_code, status.HTTP_200_OK)

        Use test_data with ddt as decorator:

            @ddt
            class MyTest(TransactionTestCase):

                @test_data('staff', 'admin', 'owner', 'manager')
                def test_user_can_reach_project(self, user):
                    self.client.force_authenticate(user=user)
                    url = factories.ProjectFactory.get_url(test_data.project)
                    response = self.client.get(url)
                    self.assertEqual(response.status_code, status.HTTP_200_OK)
    """

    def __init__(self, *args):
        self.args = args

    def __call__(self, method):
        @data(*self.args)
        def wrapped(self, *method_args):
            method_args = [getattr(test_data, arg) for arg in method_args]
            return method(self, *method_args)
        return wrapped

    @cached_class_property
    def customer(cls):
        return factories.CustomerFactory()

    @cached_class_property
    def service_settings(cls):
        return factories.ServiceSettingsFactory(customer=cls.customer)

    @cached_class_property
    def project(cls):
        return factories.ProjectFactory(customer=cls.customer)

    @cached_class_property
    def service(cls):
        return factories.TestServiceFactory(service_settings=cls.service_settings, customer=cls.customer)

    @cached_class_property
    def service_project_link(cls):
        return factories.TestServiceProjectLinkFactory(service=cls.service, project=cls.project)

    @cached_class_property
    def instance(cls):
        return factories.TestInstanceFactory(service_project_link=cls.service_project_link)

    @cached_class_property
    def staff(cls):
        return factories.UserFactory(is_staff=True)

    @cached_class_property
    def user(cls):
        return factories.UserFactory()

    @cached_class_property
    def owner(cls):
        owner = factories.UserFactory()
        cls.customer.add_user(owner, models.CustomerRole.OWNER)
        return owner

    @cached_class_property
    def admin(cls):
        admin = factories.UserFactory()
        cls.project.add_user(admin, models.ProjectRole.ADMINISTRATOR)
        return admin

    @cached_class_property
    def manager(cls):
        manager = factories.UserFactory()
        cls.project.add_user(manager, models.ProjectRole.MANAGER)
        return manager
<commit_msg>Make test_data inheritable<commit_after>from ddt import data
from django.core.exceptions import ObjectDoesNotExist

from . import factories
from .. import models

class cached_class_property(object):
    """ Allows to reach classmethod as property and cache returned value in class """

    def __init__(self, method):
        self.method = method
        self.cache_name = self.method.__name__ + '_cached'

    def __get__(self, owner_self, owner_cls):
        try:
            value = getattr(owner_cls, self.cache_name)
            value.refresh_from_db()
        except (AttributeError, ObjectDoesNotExist):
            value = self.method(owner_cls)
            setattr(owner_cls, self.cache_name, value)
        return value

class test_data(object):
    """ Container with standard structure objects that can be used for testing.

        Use test_data as enum:

            def test_admin_can_reach_project(self):
                self.client.force_authenticate(user=test_data.admin)
                url = factories.ProjectFactory.get_url(test_data.project)
                response = self.client.get(url)
                self.assertEqual(response.status_code, status.HTTP_200_OK)

        Use test_data with ddt as decorator:

            @ddt
            class MyTest(TransactionTestCase):

                @test_data('staff', 'admin', 'owner', 'manager')
                def test_user_can_reach_project(self, user):
                    self.client.force_authenticate(user=user)
                    url = factories.ProjectFactory.get_url(test_data.project)
                    response = self.client.get(url)
                    self.assertEqual(response.status_code, status.HTTP_200_OK)
    """

    def __init__(self, *args):
        self.args = args

    def __call__(self, method):
        cls = self.__class__

        @data(*self.args)
        def wrapped(self, *method_args):
            method_args = [getattr(cls, arg) for arg in method_args]
            return method(self, *method_args)
        return wrapped

    @cached_class_property
    def customer(cls):
        return factories.CustomerFactory()

    @cached_class_property
    def service_settings(cls):
        return factories.ServiceSettingsFactory(customer=cls.customer)

    @cached_class_property
    def project(cls):
        return factories.ProjectFactory(customer=cls.customer)

    @cached_class_property
    def service(cls):
        return factories.TestServiceFactory(service_settings=cls.service_settings, customer=cls.customer)

    @cached_class_property
    def service_project_link(cls):
        return factories.TestServiceProjectLinkFactory(service=cls.service, project=cls.project)

    @cached_class_property
    def instance(cls):
        return factories.TestInstanceFactory(service_project_link=cls.service_project_link)

    @cached_class_property
    def staff(cls):
        return factories.UserFactory(is_staff=True)

    @cached_class_property
    def user(cls):
        return factories.UserFactory()

    @cached_class_property
    def owner(cls):
        owner = factories.UserFactory()
        cls.customer.add_user(owner, models.CustomerRole.OWNER)
        return owner

    @cached_class_property
    def admin(cls):
        admin = factories.UserFactory()
        cls.project.add_user(admin, models.ProjectRole.ADMINISTRATOR)
        return admin

    @cached_class_property
    def manager(cls):
        manager = factories.UserFactory()
        cls.project.add_user(manager, models.ProjectRole.MANAGER)
        return manager
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#  _  __
# | |/ /___ ___ _ __  ___ _ _ ®
# | ' </ -_) -_) '_ \/ -_) '_|
# |_|\_\___\___| .__/\___|_|
#              |_|
#
# Keeper Commander
# Copyright 2020 Keeper Security Inc.
# Contact: ops@keepersecurity.com
#

__version__ = '4.53'
<commit_msg>Version<commit_after># -*- coding: utf-8 -*-
#  _  __
# | |/ /___ ___ _ __  ___ _ _ ®
# | ' </ -_) -_) '_ \/ -_) '_|
# |_|\_\___\___| .__/\___|_|
#              |_|
#
# Keeper Commander
# Copyright 2020 Keeper Security Inc.
# Contact: ops@keepersecurity.com
#

__version__ = '4.54'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#  _  __
# | |/ /___ ___ _ __  ___ _ _ ®
# | ' </ -_) -_) '_ \/ -_) '_|
# |_|\_\___\___| .__/\___|_|
#              |_|
#
# Keeper Commander
# Copyright 2018 Keeper Security Inc.
# Contact: ops@keepersecurity.com
#

__version__ = '4.2'
<commit_msg>Updated version<commit_after># -*- coding: utf-8 -*-
#  _  __
# | |/ /___ ___ _ __  ___ _ _ ®
# | ' </ -_) -_) '_ \/ -_) '_|
# |_|\_\___\___| .__/\___|_|
#              |_|
#
# Keeper Commander
# Copyright 2018 Keeper Security Inc.
# Contact: ops@keepersecurity.com
#

__version__ = '4.3'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#  _  __
# | |/ /___ ___ _ __  ___ _ _ ®
# | ' </ -_) -_) '_ \/ -_) '_|
# |_|\_\___\___| .__/\___|_|
#              |_|
#
# Keeper Commander
# Copyright 2020 Keeper Security Inc.
# Contact: ops@keepersecurity.com
#

__version__ = '4.76'
<commit_msg>Bumping version<commit_after># -*- coding: utf-8 -*-
#  _  __
# | |/ /___ ___ _ __  ___ _ _ ®
# | ' </ -_) -_) '_ \/ -_) '_|
# |_|\_\___\___| .__/\___|_|
#              |_|
#
# Keeper Commander
# Copyright 2020 Keeper Security Inc.
# Contact: ops@keepersecurity.com
#

__version__ = '4.77'
<|endoftext|>