<commit_before># -*- coding: utf-8 -*-
#
# © 2011 SimpleGeo, Inc. All rights reserved.
# Author: Paul Lathrop <paul@simplegeo.com>
#

"""
Command-line utility for querying/manipulating apt repositories.
"""

import sys

import clusto
from clusto import script_helper

from sgext.drivers import Apt

class AptRepository(script_helper.Script):
    """
    Command-line utility for querying/manipulating apt repositories.
    """

    _usage = """
             clusto-apt [options] reponame[/dist[/package]] action
             clusto-apt <list>

             Command-line utility for querying/manipulating apt
             repositories.

             Available actions for `clusto-apt reponame action`:

               list: List the distributions available in the
                     specified repository. This is the default
                     action if none is specified.

             Available actions for `clusto-apt reponame/dist action`:

               list: List the packages available in the specified
                     dist. This is the default action if none is
                     specified.
               versions: List the packages available in the specified
                         dist, along with their version. NOTE: This
                         takes a very long time.

             Available actions for `clusto-apt reponame/dist/package
             action`:

               version: Print the version string for the specified
                        package. This is the default action if none
                        is specified.
               metadata: Print the package metadata for the specified
                         package.

             The form `clusto-apt` (or `clusto-apt list`) will list
             the clusto IDs of known apt repositories.
             """

    def _add_arguments(self, parser):
        parser.add_argument('-d', '--dest', help='Destination dist. Required '
                            'when promoting a package, ignored otherwise.',
                            default=None)
        parser.add_argument('path', help='In the form: '
                            'reponame[/dist[/package]] - specifies the repo, '
                            'dist, or package you want to query/manipulate.',
                            nargs='?', default='list')
        parser.add_argument('action', help='The query or action you want to '
                            'invoke on the specified repository', nargs='?',
                            default='list')
        parser.usage = self._usage

    def add_subparser(self, subparsers):
        parser = self._setup_subparser(subparsers)
        self._add_arguments(parser)

    def _cmd_list(self, args):
        if args.path is None or args.reponame is None:
            repos = clusto.get_entities(clusto_types=[Apt])
            print 'Repositories in Clusto:'
            for repo in repos:
                print repo.name.rjust(len(repo.name) + 2)
            return 0
        repo = clusto.get_by_name(args.reponame)
        if args.dist is None:
            print 'Dists in repository %s:' % repo.name
            for dist in repo.dists():
                print dist.rjust(len(dist) + 2)
            return 0
        elif args.package is None:
            for pkg in repo.packages(args.dist):
                print pkg
            return 0
        else:
            return self._cmd_version(args)

    def _cmd_versions(self, args):
        repo = clusto.get_by_name(args.reponame)
        pkg_versions = repo.package_versions(args.dist)
        pkg_name_width = max([name for (name, ver) in pkg_versions],
                             key=len)
        pkg_field_width = len(max(['Package', pkg_name_width], key=len)) + 4
        print 'Package'.ljust(pkg_field_width) + 'Version'
        for (name, ver) in pkg_versions:
            print name.ljust(pkg_field_width) + ver

    def _cmd_version(self, args):
        repo = clusto.get_by_name(args.reponame)
        version = repo.package_version(args.package, args.dist)
        print args.package.ljust(len(args.package) + 4) + version
        return 0

    def _cmd_promote(self, args):
        if args.package is None:
            raise ValueError('Must specify a package to promote.')
        if args.dest is None:
            raise ValueError('Must specify a destination dist using'
                             '-d or --dest.')
        repo = clusto.get_by_name(args.reponame)
        repo.promote(args.package, args.dist, args.dest)
        return 0

    def _cmd_metadata(self, args):
        repo = clusto.get_by_name(args.reponame)
        meta = repo.package(args.package, args.dist)
        key_field_len = len(max(meta, key=len)) + 2
        for (key, val) in sorted(meta.iteritems()):
            print key.ljust(key_field_len) + val
        return 0

    def parse_path(self, path):
        result = [None, None, None]
        if path is None:
            return result
        components = path.split('/')
        for idx in range(len(components)):
            result[idx] = components[idx]
        return result

    def run(self, args):
        try:
            cmd = getattr(self, '_cmd_%s' % args.action)
            return cmd(args)
        except AttributeError:
            raise StandardError('No such command %s' % args.action)

def main():
    repo, args = script_helper.init_arguments(AptRepository)
    if args.path == 'list':
        args.path = None
        args.action = 'list'
    (args.reponame, args.dist, args.package) = repo.parse_path(args.path)
    return repo.run(args)

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>simplify parse_path per @ieure's suggestion.<commit_after># -*- coding: utf-8 -*-
#
# © 2011 SimpleGeo, Inc. All rights reserved.
# Author: Paul Lathrop <paul@simplegeo.com>
#

"""
Command-line utility for querying/manipulating apt repositories.
"""

import sys

import clusto
from clusto import script_helper

from sgext.drivers import Apt

class AptRepository(script_helper.Script):
    """
    Command-line utility for querying/manipulating apt repositories.
    """

    _usage = """
             clusto-apt [options] reponame[/dist[/package]] action
             clusto-apt <list>

             Command-line utility for querying/manipulating apt
             repositories.

             Available actions for `clusto-apt reponame action`:

               list: List the distributions available in the
                     specified repository. This is the default
                     action if none is specified.

             Available actions for `clusto-apt reponame/dist action`:

               list: List the packages available in the specified
                     dist. This is the default action if none is
                     specified.
               versions: List the packages available in the specified
                         dist, along with their version. NOTE: This
                         takes a very long time.

             Available actions for `clusto-apt reponame/dist/package
             action`:

               version: Print the version string for the specified
                        package. This is the default action if none
                        is specified.
               metadata: Print the package metadata for the specified
                         package.

             The form `clusto-apt` (or `clusto-apt list`) will list
             the clusto IDs of known apt repositories.
             """

    def _add_arguments(self, parser):
        parser.add_argument('-d', '--dest', help='Destination dist. Required '
                            'when promoting a package, ignored otherwise.',
                            default=None)
        parser.add_argument('path', help='In the form: '
                            'reponame[/dist[/package]] - specifies the repo, '
                            'dist, or package you want to query/manipulate.',
                            nargs='?', default='list')
        parser.add_argument('action', help='The query or action you want to '
                            'invoke on the specified repository', nargs='?',
                            default='list')
        parser.usage = self._usage

    def add_subparser(self, subparsers):
        parser = self._setup_subparser(subparsers)
        self._add_arguments(parser)

    def _cmd_list(self, args):
        if args.path is None or args.reponame is None:
            repos = clusto.get_entities(clusto_types=[Apt])
            print 'Repositories in Clusto:'
            for repo in repos:
                print repo.name.rjust(len(repo.name) + 2)
            return 0
        repo = clusto.get_by_name(args.reponame)
        if args.dist is None:
            print 'Dists in repository %s:' % repo.name
            for dist in repo.dists():
                print dist.rjust(len(dist) + 2)
            return 0
        elif args.package is None:
            for pkg in repo.packages(args.dist):
                print pkg
            return 0
        else:
            return self._cmd_version(args)

    def _cmd_versions(self, args):
        repo = clusto.get_by_name(args.reponame)
        pkg_versions = repo.package_versions(args.dist)
        pkg_name_width = max([name for (name, ver) in pkg_versions],
                             key=len)
        pkg_field_width = len(max(['Package', pkg_name_width], key=len)) + 4
        print 'Package'.ljust(pkg_field_width) + 'Version'
        for (name, ver) in pkg_versions:
            print name.ljust(pkg_field_width) + ver

    def _cmd_version(self, args):
        repo = clusto.get_by_name(args.reponame)
        version = repo.package_version(args.package, args.dist)
        print args.package.ljust(len(args.package) + 4) + version
        return 0

    def _cmd_promote(self, args):
        if args.package is None:
            raise ValueError('Must specify a package to promote.')
        if args.dest is None:
            raise ValueError('Must specify a destination dist using'
                             '-d or --dest.')
        repo = clusto.get_by_name(args.reponame)
        repo.promote(args.package, args.dist, args.dest)
        return 0

    def _cmd_metadata(self, args):
        repo = clusto.get_by_name(args.reponame)
        meta = repo.package(args.package, args.dist)
        key_field_len = len(max(meta, key=len)) + 2
        for (key, val) in sorted(meta.iteritems()):
            print key.ljust(key_field_len) + val
        return 0

    def parse_path(self, path):
        result = [None, None, None]
        if path is None:
            return result
        return (path.split('/')[:3] + result)[:3]

    def run(self, args):
        try:
            cmd = getattr(self, '_cmd_%s' % args.action)
            return cmd(args)
        except AttributeError:
            raise StandardError('No such command %s' % args.action)

def main():
    repo, args = script_helper.init_arguments(AptRepository)
    if args.path == 'list':
        args.path = None
        args.action = 'list'
    (args.reponame, args.dist, args.package) = repo.parse_path(args.path)
    return repo.run(args)

if __name__ == '__main__':
    sys.exit(main())
<|endoftext|>


<commit_before>from kivy.clock import Clock

import time

from defines import *

import ai_genome as aig_m
import ai_factory as f_m
import human_player as h_m
import players_mgr as pm_m
import rules as r_m
import game as g_m

class Demo():
    def __init__(self, app, size):
        self.app = app
        self.size = size

        # TODO Save settings
        # for settings demo

        # TODO Intercept Input

        self.script = self.rules_script()

    def play(self, dt=None):
        print "A"
        '''
        for i in range(20):
            now = time.time()
        '''

        try:
            print "B"
            sleep_time = self.script.next()
            print "C"
        except StopIteration:
            print "D"
            self.finish()
            return

        print "E"
        if sleep_time != None:
            print "F, %s" % sleep_time
            Clock.schedule_once(self.play, sleep_time)
            print "G"

    def finish(self):
        self.app.show_menu_screen()

    def rules_script(self):
        '''
        self.app.show_menu_screen()
        yield(2)
        '''
        #self.app.menu_screen.ids.new_game.state = "down"
        #yield(1)
        #self.app.menu_screen.ids.new_game.state = "normal"
        #yield(1)

        self.app.show_new_game_screen()
        yield(2)

        pmgr = pm_m.PlayersMgr()

        human = pmgr.get_recent_player_names("Human", 1)[0]
        p1 = h_m.HumanPlayer(human) # TODO: Use most recent human player name

        aif = f_m.AIFactory()
        genome = aig_m.AIGenome("PentAI")
        genome.use_openings_book = False
        p2 = aif.create_player(genome)

        # Create a game
        r = r_m.Rules(13, "standard")
        game = g_m.Game(r, p1, p2)

        yield(2)
        # And start it...
        #st()

        # demo flag: Disable prompting of players
        self.app.start_game(game, [457, 720], demo=True) # size is a HACK
        yield(1.2) # Compensating for .7 wait at start?!

        def mm(x,y):
            game.make_move((x,y))
            self.app.pente_screen.perform(0)
            self.app.pente_screen.refresh_all()

        mm(3,4)
        yield(.5)

        mm(6,8)
        yield(.5)
        mm(4,4)
        yield(.5)

        mm(6,7)
        yield(.5)
        mm(5,8)
        yield(.5)

        mm(6,9)
        yield(.5)
        mm(8,3)
        yield(.5)

        mm(6,10)
        yield(5)

        # Diagonal white win
        game.go_to_the_beginning()
        yield(2)

        mm(6, 6)
        yield(.5)
        mm(5, 5)
        yield(.5)

        mm(4, 6)
        yield(.5)
        mm(7, 3)
        yield(.5)

        mm(7, 5)
        yield(.5)
        mm(9, 1)
        yield(.5)

        mm(5, 6)
        yield(.5)
        mm(6, 4)
        yield(.5)

        mm(8, 6)
        yield(1)
        mm(8, 2)
        yield(5)

        # Captures
        game.go_to_the_beginning()
        yield(2)

        mm(6, 6)
        yield(.5)
        mm(6, 7)
        yield(.5)

        mm(6, 5)
        yield(1.5)
        mm(6, 4)
        yield(2.5)

        mm(5, 3)
        yield(.5)
        mm(7, 5)
        yield(1.5)

        mm(8, 6)
        yield(1.5)
        mm(6, 6)
        yield(3)

        # Non captures
        ##############
        game.go_to_the_beginning()
        yield(2)

        # Length 1
        mm(6, 6)
        yield(.5)
        mm(0, 0)
        yield(.5)

        mm(0, 1)
        yield(1)
        mm(0, 2)
        yield(1)

        # Length 3
        mm(2, 1)
        yield(.5)
        mm(2, 0)
        yield(.5)

        mm(2, 3)
        yield(.5)
        mm(8, 4)
        yield(.5)

        mm(2, 2)
        yield(1)
        mm(2, 4)
        yield(3)

        # Place inside the border
        mm(4, 1)
        yield(.5)
        mm(4, 0)
        yield(.5)

        mm(4, 10)
        yield(.5)
        mm(4, 3)
        yield(2)

        mm(4, 2) # Black places inside, doesn't get taken
        yield(4)
        mm(9, 7)
        yield(1)

        # Gap
        mm(6, 3)
        yield(.5)
        mm(6, 0)
        yield(.5)

        mm(6, 1)
        yield(1)
        mm(6, 4)
        yield(5)

        # 5 Pairs wins
        game.go_to_the_beginning()
        yield(2)

        # 1
        mm(0, 1)
        yield(.5)
        mm(0, 0)
        yield(.5)

        mm(0, 2)
        yield(.5)
        mm(0, 3)
        yield(.5)

        # 2
        mm(2, 1)
        yield(.5)
        mm(2, 0)
        yield(.5)

        mm(2, 2)
        yield(.5)
        mm(2, 3)
        yield(.5)

        # 3
        mm(4, 2)
        yield(.5)
        mm(4, 0)
        yield(.5)

        mm(4, 1)
        yield(.5)
        mm(4, 3)
        yield(.5)

        # 4
        mm(7, 1)
        yield(.5)
        mm(6, 0)
        yield(.5)

        mm(8, 2)
        yield(.5)
        mm(9, 3)
        yield(.5)

        # 5
        mm(2, 10)
        yield(.5)
        mm(0, 12)
        yield(.5)

        mm(1, 11)
        yield(2)
        mm(3, 9)
        yield(4)

<commit_msg>Cleanup, minor script change<commit_after>from kivy.clock import Clock

import time

from defines import *

import ai_genome as aig_m
import ai_factory as f_m
import human_player as h_m
import players_mgr as pm_m
import rules as r_m
import game as g_m

class Demo():
    def __init__(self, app, size):
        self.app = app
        self.size = size

        # TODO Save settings
        # for settings demo

        # TODO Intercept Input

        self.script = self.rules_script()

    def play(self, dt=None):
        try:
            sleep_time = self.script.next()
        except StopIteration:
            self.finish()
            return

        if sleep_time != None:
            Clock.schedule_once(self.play, sleep_time)

    def finish(self):
        self.app.show_menu_screen()

    def rules_script(self):
        '''
        self.app.show_menu_screen()
        yield(2)
        '''
        #self.app.menu_screen.ids.new_game.state = "down"
        #yield(1)
        #self.app.menu_screen.ids.new_game.state = "normal"
        #yield(1)

        self.app.show_new_game_screen()
        yield(2)

        pmgr = pm_m.PlayersMgr()

        human = pmgr.get_recent_player_names("Human", 1)[0]
        p1 = h_m.HumanPlayer(human) # TODO: Use most recent human player name

        aif = f_m.AIFactory()
        genome = aig_m.AIGenome("PentAI")
        genome.use_openings_book = False
        p2 = aif.create_player(genome)

        # Create a game
        r = r_m.Rules(13, "standard")
        game = g_m.Game(r, p1, p2)

        yield(2)
        # And start it...
        #st()

        # demo flag: Disable prompting of players
        self.app.start_game(game, [457, 720], demo=True) # size is a HACK
        yield(1.2) # Compensating for .7 wait at start?!

        def mm(x,y):
            game.make_move((x,y))
            self.app.pente_screen.perform(0)
            self.app.pente_screen.refresh_all()

        mm(3,4)
        yield(.5)

        mm(6,8)
        yield(.5)
        mm(4,4)
        yield(.5)

        mm(6,7)
        yield(.5)
        mm(5,8)
        yield(.5)

        mm(6,9)
        yield(.5)
        mm(8,3)
        yield(.5)

        mm(6,10)
        yield(5)

        # Diagonal white win
        game.go_to_the_beginning()
        yield(2)

        mm(6, 6)
        yield(.5)
        mm(5, 5)
        yield(.5)

        mm(4, 6)
        yield(.5)
        mm(7, 3)
        yield(.5)

        mm(7, 5)
        yield(.5)
        mm(9, 1)
        yield(.5)

        mm(5, 6)
        yield(.5)
        mm(6, 4)
        yield(.5)

        mm(8, 6)
        yield(1)
        mm(8, 2)
        yield(5)

        # Captures
        game.go_to_the_beginning()
        yield(2)

        mm(6, 6)
        yield(.5)
        mm(6, 7)
        yield(.5)

        mm(6, 5)
        yield(1.5)
        mm(6, 4)
        yield(2.5)

        mm(5, 3)
        yield(.5)
        mm(7, 5)
        yield(1.5)

        mm(8, 6)
        yield(1.5)
        mm(6, 6)
        yield(3)

        # Non captures
        ##############
        game.go_to_the_beginning()
        yield(2)

        # Length 1
        mm(6, 6)
        yield(.5)
        mm(0, 0)
        yield(.5)

        mm(0, 1)
        yield(1)
        mm(0, 2)
        yield(1)

        # Length 3
        mm(2, 1)
        yield(.5)
        mm(2, 0)
        yield(.5)

        mm(2, 3)
        yield(.5)
        mm(8, 4)
        yield(.5)

        mm(2, 2)
        yield(1)
        mm(2, 4)
        yield(3)

        # Place inside the border
        mm(4, 1)
        yield(.5)
        mm(4, 0)
        yield(.5)

        mm(4, 10)
        yield(.5)
        mm(4, 3)
        yield(2)

        mm(4, 2) # Black places inside, doesn't get taken
        yield(4)
        mm(9, 7)
        yield(1)

        # Gap
        mm(6, 3)
        yield(.5)
        mm(6, 0)
        yield(.5)

        mm(6, 1)
        yield(1)
        mm(6, 4)
        yield(5)

        # 5 Pairs wins
        game.go_to_the_beginning()
        yield(2)

        # 1
        mm(0, 1)
        yield(.5)
        mm(0, 0)
        yield(.5)

        mm(0, 2)
        yield(.5)
        mm(0, 3)
        yield(.5)

        # 2
        mm(2, 1)
        yield(.5)
        mm(2, 0)
        yield(.5)

        mm(2, 2)
        yield(.5)
        mm(2, 3)
        yield(.5)

        # 3
        mm(4, 2)
        yield(.5)
        mm(4, 3)
        yield(.5)

        mm(4, 1)
        yield(.5)
        mm(4, 0)
        yield(.5)

        # 4
        mm(7, 1)
        yield(.5)
        mm(6, 0)
        yield(.5)

        mm(8, 2)
        yield(.5)
        mm(9, 3)
        yield(.5)

        # 5
        mm(2, 10)
        yield(.5)
        mm(0, 12)
        yield(.5)

        mm(1, 11)
        yield(2)
        mm(3, 9)
        yield(4)

<|endoftext|>


<commit_before>import click
import json
import os
import sys

from shakedown.cli.helpers import *

@click.command('shakedown')
@click.argument('path', type=click.Path(exists=True), nargs=-1)
@click.option('--dcos_url', help='URL to a running DCOS cluster.')
@click.option('--fail', type=click.Choice(['fast', 'never']), default='fast', help='Sepcify whether to continue testing when encountering failures. (default: fast)')
@click.option('--ssh_key_file', type=click.Path(), help='Path to the SSH keyfile to use for authentication')
@click.option('--no_banner', is_flag=True, help='Suppress the product banner.')
@click.option('--quiet', is_flag=True, help='Suppress all superfluous output.')
@click.option('--report', type=click.Choice(['json', 'junit']), help='Return a report in the specified format.')
@click.option('--stdout', type=click.Choice(['pass', 'fail', 'skip', 'all', 'none']), default='fail', help='Print the standard output of tests with the specified result. (default: fail)')
@click.version_option(version=shakedown.VERSION)

def cli(**args):
    """ Main CLI entry-point; perform pre-flight and parse arguments
    """
    import shakedown

    if args['quiet']:
        shakedown.cli.quiet = True

    if not args['dcos_url']:
        click.secho('error: --dcos_url is a required option; see --help for more information.', fg='red', bold=True)
        exit(1)

    if args['ssh_key_file']:
        shakedown.cli.ssh_key_file = args['ssh_key_file']

    if not args['no_banner']:
        echo(banner(), n=False)

    echo('Running pre-flight checks...', d='step-maj')
    echo('Checking for pytest library...', d='step-min', n=False)
    try:
        import pytest
    except ImportError:
        click.secho("error: pytest is not installed; run 'pip install pytest'.", fg='red', bold=True)
        exit(1)
    echo(pytest.__version__)
    echo('Checking for dcos library...', d='step-min', n=False)
    try:
        import dcos
    except ImportError:
        click.secho("error: dcos is not installed; run 'pip install dcos'.", fg='red', bold=True)
        exit(1)
    echo(dcos.version)
    echo('Checking DCOS cluster state...', d='step-min', n=False)
    try:
        from dcos import (config, marathon, util)

        with stdchannel_redirected(sys.stdout, os.devnull):
            config.set_val('core.dcos_url', args['dcos_url'])

        with stdchannel_redirected(sys.stderr, os.devnull):
            dcos_config = util.get_config()
            init_client = marathon.create_client(dcos_config)
    except:
        click.secho("error: cluster '" + args['dcos_url'] + "' is unreachable.", fg='red', bold=True)
        exit(1)
    echo('running')

    class shakedown:
        """ This encapsulates a PyTest wrapper plugin
        """

        state = {}

        tests = {
            'file': {},
            'test': {}
        }

        report_stats = {
            'passed':[],
            'skipped':[],
            'failed':[],
            'total_passed':0,
            'total_skipped':0,
            'total_failed':0,
        }

        def pytest_collectreport(self, report):
            """ Collect and validate individual test files
            """

            if not 'collect' in shakedown.state:
                shakedown.state['collect'] = 1
                echo('Collecting and validating test files...', d='step-min')

            if report.nodeid:
                echo(report.nodeid, d='item-maj', n=False)

                if report.failed:
                    echo('import fail', d='fail')
                else:
                    echo(chr(10003), d='pass')

                if args['stdout'] and report.longrepr:
                    state = None

                    if report.failed and args['stdout'] in ['fail', 'all']:
                        state = 'fail'
                    if report.passed and args['stdout'] in ['pass', 'all']:
                        state = 'pass'
                    if report.skipped and args['stdout'] in ['skip', 'all']:
                        state = 'skip'

                    if state:
                        try:
                            shakedown.tests['test'][report.nodeid]
                        except KeyError:
                            shakedown.tests['test'][report.nodeid] = {}

                        shakedown.tests['test'][report.nodeid][state] = str(report.longrepr).rstrip()

        def pytest_sessionstart(self):
            """ Tests have been collected, begin running them...
            """

            echo('Initiating testing phase...', d='step-maj')

        def pytest_report_teststatus(self, report):
            """ Print report results to the console as they are run
            """

            try:
                report_file, report_test = report.nodeid.split('::', 1)
            except ValueError:
                return

            if not 'test' in shakedown.state:
                shakedown.state['test'] = 1
                echo('Running individual tests...', d='step-min')

            if not report_file in shakedown.tests['file']:
                shakedown.tests['file'][report_file] = 1
                echo(report_file, d='item-maj')
            if not report.nodeid in shakedown.tests['test']:
                shakedown.tests['test'][report.nodeid] = {}
                echo(report_test, d='item-min', n=False)

            if report.passed:
                echo(report.when, d='pass', n=False)
            elif report.skipped:
                echo(report.when, d='skip', n=False)
            elif report.failed:
                echo(report.when, d='fail', n=False)

            if report.when == 'teardown':
                echo('')

            # Suppress excess terminal output
            return report.outcome, None, None

        def pytest_runtest_logreport(self, report):
            """ Log the [stdout, stderr] results of tests if desired
            """

            if args['stdout']:
                state = None

                for secname, content in report.sections:
                    if report.failed and args['stdout'] in ['fail', 'all']:
                        state = 'fail'
                    if (report.passed) and args['stdout'] in ['pass', 'all']:
                        state = 'pass'
                    if report.skipped and args['stdout'] in ['skip', 'all']:
                        state = 'skip'

                    if state and report.when == 'call':
                        if not state in shakedown.tests['test'][report.nodeid]:
                            shakedown.tests['test'][report.nodeid][state] = content
                        else:
                            shakedown.tests['test'][report.nodeid][state] += content

                # Capture execution crashes
                if hasattr(report.longrepr, 'reprcrash'):
                    longreport = report.longrepr

                    if not 'fail' in shakedown.tests['test'][report.nodeid]:
                        shakedown.tests['test'][report.nodeid]['fail'] = 'error: ' + str(longreport.reprcrash)
                    else:
                        shakedown.tests['test'][report.nodeid]['fail'] += 'error: ' + str(longreport.reprcrash)

        def pytest_runtest_makereport(self, item, call, __multicall__):
            """ Store "simple" (pass, fail, skip) test results
            """

            report = __multicall__.execute()

            # Put job run report data into shakedown.report_status hash
            if report.passed:
                shakedown.report_stats['passed'].append(report.nodeid + '.' + report.when)
                shakedown.report_stats['total_passed'] += 1
            if report.failed:
                shakedown.report_stats['failed'].append(report.nodeid + '.' + report.when)
                shakedown.report_stats['total_failed'] += 1
            if report.skipped:
                shakedown.report_stats['skipped'].append(report.nodeid + '.' + report.when)
                shakedown.report_stats['total_skipped'] += 1

            return report

        def pytest_sessionfinish(self, session, exitstatus):
            """ Testing phase is complete; print extra reports (stdout/stderr, JSON) as requested
            """

            echo('Test phase completed.', d='step-maj')

            if ('stdout' in args and args['stdout']) and \
                    ('test' in shakedown.tests and shakedown.tests['test']):
                for test in sorted(shakedown.tests['test']):
                    for result in ['fail', 'pass', 'skip']:
                        if result in shakedown.tests['test'][test]:
                            echo('Output during ', d='quote-head-' + result, n=False)

                            shakedown.tests['test'][test][result] = shakedown.tests['test'][test][result].strip()

                            if 'quiet' in args and args['quiet']:
                                print('-' * len(test) + "\n" + test + "\n" + '-' * len(test))
                                print(shakedown.tests['test'][test][result])
                            else:
                                click.secho(decorate(test, style=result), bold=True)
                                click.echo(decorate(shakedown.tests['test'][test][result], style='quote-' + result))

            if args['report'] == 'json':
                click.echo("\n" + json.dumps(shakedown.report_stats, sort_keys=True, indent=4, separators=(',', ': ')))

    opts = ['-q', '--tb=no']

    if args['fail'] == 'fast':
        opts.append('-x')

    if args['path']:
        opts.append(' '.join(args['path']))

    pytest.main(' '.join(opts), plugins=[shakedown()])
<commit_msg>s/stdout/stderr/<commit_after>import click
import json
import os
import sys

from shakedown.cli.helpers import *

@click.command('shakedown')
@click.argument('path', type=click.Path(exists=True), nargs=-1)
@click.option('--dcos_url', help='URL to a running DCOS cluster.')
@click.option('--fail', type=click.Choice(['fast', 'never']), default='fast', help='Sepcify whether to continue testing when encountering failures. (default: fast)')
@click.option('--ssh_key_file', type=click.Path(), help='Path to the SSH keyfile to use for authentication')
@click.option('--no_banner', is_flag=True, help='Suppress the product banner.')
@click.option('--quiet', is_flag=True, help='Suppress all superfluous output.')
@click.option('--report', type=click.Choice(['json', 'junit']), help='Return a report in the specified format.')
@click.option('--stdout', type=click.Choice(['pass', 'fail', 'skip', 'all', 'none']), default='fail', help='Print the standard output of tests with the specified result. (default: fail)')
@click.version_option(version=shakedown.VERSION)

def cli(**args):
    """ Main CLI entry-point; perform pre-flight and parse arguments
    """
    import shakedown

    if args['quiet']:
        shakedown.cli.quiet = True

    if not args['dcos_url']:
        click.secho('error: --dcos_url is a required option; see --help for more information.', fg='red', bold=True)
        exit(1)

    if args['ssh_key_file']:
        shakedown.cli.ssh_key_file = args['ssh_key_file']

    if not args['no_banner']:
        echo(banner(), n=False)

    echo('Running pre-flight checks...', d='step-maj')
    echo('Checking for pytest library...', d='step-min', n=False)
    try:
        import pytest
    except ImportError:
        click.secho("error: pytest is not installed; run 'pip install pytest'.", fg='red', bold=True)
        exit(1)
    echo(pytest.__version__)
    echo('Checking for dcos library...', d='step-min', n=False)
    try:
        import dcos
    except ImportError:
        click.secho("error: dcos is not installed; run 'pip install dcos'.", fg='red', bold=True)
        exit(1)
    echo(dcos.version)
    echo('Checking DCOS cluster state...', d='step-min', n=False)
    try:
        from dcos import (config, marathon, util)

        with stdchannel_redirected(sys.stderr, os.devnull):
            config.set_val('core.dcos_url', args['dcos_url'])

        with stdchannel_redirected(sys.stderr, os.devnull):
            dcos_config = util.get_config()
            init_client = marathon.create_client(dcos_config)
    except:
        click.secho("error: cluster '" + args['dcos_url'] + "' is unreachable.", fg='red', bold=True)
        exit(1)
    echo('running')

    class shakedown:
        """ This encapsulates a PyTest wrapper plugin
        """

        state = {}

        tests = {
            'file': {},
            'test': {}
        }

        report_stats = {
            'passed':[],
            'skipped':[],
            'failed':[],
            'total_passed':0,
            'total_skipped':0,
            'total_failed':0,
        }

        def pytest_collectreport(self, report):
            """ Collect and validate individual test files
            """

            if not 'collect' in shakedown.state:
                shakedown.state['collect'] = 1
                echo('Collecting and validating test files...', d='step-min')

            if report.nodeid:
                echo(report.nodeid, d='item-maj', n=False)

                if report.failed:
                    echo('import fail', d='fail')
                else:
                    echo(chr(10003), d='pass')

                if args['stdout'] and report.longrepr:
                    state = None

                    if report.failed and args['stdout'] in ['fail', 'all']:
                        state = 'fail'
                    if report.passed and args['stdout'] in ['pass', 'all']:
                        state = 'pass'
                    if report.skipped and args['stdout'] in ['skip', 'all']:
                        state = 'skip'

                    if state:
                        try:
                            shakedown.tests['test'][report.nodeid]
                        except KeyError:
                            shakedown.tests['test'][report.nodeid] = {}

                        shakedown.tests['test'][report.nodeid][state] = str(report.longrepr).rstrip()

        def pytest_sessionstart(self):
            """ Tests have been collected, begin running them...
            """

            echo('Initiating testing phase...', d='step-maj')

        def pytest_report_teststatus(self, report):
            """ Print report results to the console as they are run
            """

            try:
                report_file, report_test = report.nodeid.split('::', 1)
            except ValueError:
                return

            if not 'test' in shakedown.state:
                shakedown.state['test'] = 1
                echo('Running individual tests...', d='step-min')

            if not report_file in shakedown.tests['file']:
                shakedown.tests['file'][report_file] = 1
                echo(report_file, d='item-maj')
            if not report.nodeid in shakedown.tests['test']:
                shakedown.tests['test'][report.nodeid] = {}
                echo(report_test, d='item-min', n=False)

            if report.passed:
                echo(report.when, d='pass', n=False)
            elif report.skipped:
                echo(report.when, d='skip', n=False)
            elif report.failed:
                echo(report.when, d='fail', n=False)

            if report.when == 'teardown':
                echo('')

            # Suppress excess terminal output
            return report.outcome, None, None

        def pytest_runtest_logreport(self, report):
            """ Log the [stdout, stderr] results of tests if desired
            """

            if args['stdout']:
                state = None

                for secname, content in report.sections:
                    if report.failed and args['stdout'] in ['fail', 'all']:
                        state = 'fail'
                    if (report.passed) and args['stdout'] in ['pass', 'all']:
                        state = 'pass'
                    if report.skipped and args['stdout'] in ['skip', 'all']:
                        state = 'skip'

                    if state and report.when == 'call':
                        if not state in shakedown.tests['test'][report.nodeid]:
                            shakedown.tests['test'][report.nodeid][state] = content
                        else:
                            shakedown.tests['test'][report.nodeid][state] += content

                # Capture execution crashes
                if hasattr(report.longrepr, 'reprcrash'):
                    longreport = report.longrepr

                    if not 'fail' in shakedown.tests['test'][report.nodeid]:
                        shakedown.tests['test'][report.nodeid]['fail'] = 'error: ' + str(longreport.reprcrash)
                    else:
                        shakedown.tests['test'][report.nodeid]['fail'] += 'error: ' + str(longreport.reprcrash)

        def pytest_runtest_makereport(self, item, call, __multicall__):
            """ Store "simple" (pass, fail, skip) test results
            """

            report = __multicall__.execute()

            # Put job run report data into shakedown.report_status hash
            if report.passed:
                shakedown.report_stats['passed'].append(report.nodeid + '.' + report.when)
                shakedown.report_stats['total_passed'] += 1
            if report.failed:
                shakedown.report_stats['failed'].append(report.nodeid + '.' + report.when)
                shakedown.report_stats['total_failed'] += 1
            if report.skipped:
                shakedown.report_stats['skipped'].append(report.nodeid + '.' + report.when)
                shakedown.report_stats['total_skipped'] += 1

            return report

        def pytest_sessionfinish(self, session, exitstatus):
            """ Testing phase is complete; print extra reports (stdout/stderr, JSON) as requested
            """

            echo('Test phase completed.', d='step-maj')

            if ('stdout' in args and args['stdout']) and \
                    ('test' in shakedown.tests and shakedown.tests['test']):
                for test in sorted(shakedown.tests['test']):
                    for result in ['fail', 'pass', 'skip']:
                        if result in shakedown.tests['test'][test]:
                            echo('Output during ', d='quote-head-' + result, n=False)

                            shakedown.tests['test'][test][result] = shakedown.tests['test'][test][result].strip()

                            if 'quiet' in args and args['quiet']:
                                print('-' * len(test) + "\n" + test + "\n" + '-' * len(test))
                                print(shakedown.tests['test'][test][result])
                            else:
                                click.secho(decorate(test, style=result), bold=True)
                                click.echo(decorate(shakedown.tests['test'][test][result], style='quote-' + result))

            if args['report'] == 'json':
                click.echo("\n" + json.dumps(shakedown.report_stats, sort_keys=True, indent=4, separators=(',', ': ')))

    opts = ['-q', '--tb=no']

    if args['fail'] == 'fast':
        opts.append('-x')

    if args['path']:
        opts.append(' '.join(args['path']))

    pytest.main(' '.join(opts), plugins=[shakedown()])
<|endoftext|>


<commit_before>import random
import config
from database import DatabaseManager
from recommender import KeywordRecommender
from bottle import Bottle, run, request, view, static_file

class get_dbm(object):
    def __enter__(self):
        self.dbm = DatabaseManager(config.DB_HOST, config.DB_USER, config.DB_PASSWORD, config.DB_NAME)
        return self.dbm

    def __exit__(self, type, value, traceback):
        self.dbm.close()

# Real business begins
app = Bottle()

# Serve static files
STATIC_DIRECTORY = './demo-static'
@app.route('/static/<filepath:path>')
def serve_static(filepath):
    return static_file(filepath, root=STATIC_DIRECTORY)

@app.route('/')
@view('home')
def home():
    with get_dbm() as dbm:
        group = random.randint(1, 10)
        query = 'SELECT query FROM query WHERE group_id = %d ORDER BY RAND() LIMIT 10' % group
        queries = [row['query'] for row in dbm.get_rows(query)]
        products = transfrom_results(get_hot_products(dbm))

    if not queries:
        print 'No quries selected', group

    return {
        'queries': queries,
        'products': products,
    }

@app.post('/update')
@view('recommendations')
def update_suggestions():
    query = request.POST.query
    with get_dbm() as dbm:
        recommender = KeywordRecommender(config.N, dbm, config.WordSegmenter(), None)
        products = [rec[0] for rec in recommender.recommend(query)]

        # make sure sufficient number of products is recommended
        rec_num = len(products)
        if rec_num < config.N:
            products.extend(get_hot_products(dbm)[:config.N-rec_num])

        products = transfrom_results(products)
    return {'products': products}

def get_hot_products(dbm):
    # source hot_cache.sql to make things go smoothly
    hot_product_query = 'SELECT product FROM hot_cache ORDER BY weight DESC LIMIT %d' % config.N
    products = [row['product'] for row in dbm.get_rows(hot_product_query)]
    return products

def get_image_url(p):
    i = abs(p.__hash__()) % 10 + 1
    return '/static/images/glasses%d.jpg' % i

def transfrom_results(products):
    return [(p, get_image_url(p)) for p in products]

run(app, host='localhost', port=8080, debug=True)
<commit_msg>update demo<commit_after>import random
import config
from database import DatabaseManager
from recommender import KeywordRecommender
from bottle import Bottle, run, request, view, static_file

N = 5

class get_dbm(object):
    def __enter__(self):
        self.dbm = DatabaseManager(config.DB_HOST, config.DB_USER, config.DB_PASSWORD, config.DB_NAME)
        return self.dbm

    def __exit__(self, type, value, traceback):
        self.dbm.close()

# Real business begins
app = Bottle()

# Serve static files
STATIC_DIRECTORY = './demo-static'
@app.route('/static/<filepath:path>')
def serve_static(filepath):
    return static_file(filepath, root=STATIC_DIRECTORY)

@app.route('/')
@view('home')
def home():
    with get_dbm() as dbm:
        group = random.randint(1, 10)
        query = 'SELECT query FROM query WHERE group_id = %d ORDER BY RAND() LIMIT 10' % group
        queries = [row['query'] for row in dbm.get_rows(query)]
        products = transfrom_results(get_hot_products(dbm))

    if not queries:
        print 'No quries selected', group

    return {
        'queries': queries,
        'products': products,
    }

@app.post('/update')
@view('recommendations')
def update_suggestions():
    query = request.POST.query
    with get_dbm() as dbm:
        recommender = KeywordRecommender(dbm, config.WordSegmenter(), None)
        recommender.set_limit(N)
        products = [rec[0] for rec in recommender.recommend(query)]

        # make sure sufficient number of products is recommended
        rec_num = len(products)
        if rec_num < N:
            products.extend(get_hot_products(dbm)[:N-rec_num])

        products = transfrom_results(products)
    return {'products': products}

def get_hot_products(dbm):
    # source hot_cache.sql to make things go smoothly
    hot_product_query = 'SELECT product FROM hot_cache ORDER BY weight DESC LIMIT %d' % N
    products = [row['product'] for row in dbm.get_rows(hot_product_query)]
    return products

def get_image_url(p):
    i = abs(p.__hash__()) % 10 + 1
    return '/static/images/glasses%d.jpg' % i

def transfrom_results(products):
    return [(p, get_image_url(p)) for p in products]

run(app, host='localhost', port=8080, debug=True)
<|endoftext|>


<commit_before># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://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.
"""A demo script showing how to use the uisrnn package on toy data."""

import numpy as np

import uisrnn

SAVED_MODEL_NAME = 'saved_model.uisrnn'

def diarization_experiment(model_args, training_args, inference_args):
  """Experiment pipeline.

  Load data --> train model --> test model --> output result

  Args:
    model_args: model configurations
    training_args: training configurations
    inference_args: inference configurations
  """

  predicted_labels = []
  test_record = []

  train_data = np.load('./data/toy_training_data.npz')
  test_data = np.load('./data/toy_testing_data.npz')
  train_sequence = train_data['train_sequence']
  train_cluster_id = train_data['train_cluster_id']
  test_sequences = test_data['test_sequences']
  test_cluster_ids = test_data['test_cluster_ids']

  model = uisrnn.UISRNN(model_args)

  # training
  model.fit(train_sequence, train_cluster_id, training_args)
  model.save(SAVED_MODEL_NAME)
  # we can also skip training by calling：
  # model.load(SAVED_MODEL_NAME)

  # testing
  for (test_sequence, test_cluster_id) in zip(test_sequences, test_cluster_ids):
    predicted_label = model.predict(test_sequence, inference_args)
    predicted_labels.append(predicted_label)
    accuracy = uisrnn.compute_sequence_match_accuracy(
        test_cluster_id, predicted_label)
    test_record.append((accuracy, len(test_cluster_id)))
    print('Ground truth labels:')
    print(test_cluster_id)
    print('Predicted labels:')
    print(predicted_label)
    print('-' * 80)

  output_string = uisrnn.output_result(model_args, training_args, test_record)

  print('Finished diarization experiment')
  print(output_string)

def main():
  """The main function."""
  model_args, training_args, inference_args = uisrnn.parse_arguments()
  diarization_experiment(model_args, training_args, inference_args)

if __name__ == '__main__':
  main()
<commit_msg>Some renaming and data format changes in demo.py to increase readability.<commit_after># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://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.
"""A demo script showing how to use the uisrnn package on toy data."""

import numpy as np

import uisrnn

SAVED_MODEL_NAME = 'saved_model.uisrnn'

def diarization_experiment(model_args, training_args, inference_args):
  """Experiment pipeline.

  Load data --> train model --> test model --> output result

  Args:
    model_args: model configurations
    training_args: training configurations
    inference_args: inference configurations
  """

  predicted_cluster_ids = []
  test_record = []

  train_data = np.load('./data/toy_training_data.npz')
  test_data = np.load('./data/toy_testing_data.npz')
  train_sequence = train_data['train_sequence']
  train_cluster_id = train_data['train_cluster_id']
  test_sequences = test_data['test_sequences'].tolist()
  test_cluster_ids = test_data['test_cluster_ids'].tolist()

  model = uisrnn.UISRNN(model_args)

  # training
  model.fit(train_sequence, train_cluster_id, training_args)
  model.save(SAVED_MODEL_NAME)
  # we can also skip training by calling：
  # model.load(SAVED_MODEL_NAME)

  # testing
  for (test_sequence, test_cluster_id) in zip(test_sequences, test_cluster_ids):
    predicted_cluster_id = model.predict(test_sequence, inference_args)
    predicted_cluster_ids.append(predicted_cluster_id)
    accuracy = uisrnn.compute_sequence_match_accuracy(
        test_cluster_id, predicted_cluster_id)
    test_record.append((accuracy, len(test_cluster_id)))
    print('Ground truth labels:')
    print(test_cluster_id)
    print('Predicted labels:')
    print(predicted_cluster_id)
    print('-' * 80)

  output_string = uisrnn.output_result(model_args, training_args, test_record)

  print('Finished diarization experiment')
  print(output_string)

def main():
  """The main function."""
  model_args, training_args, inference_args = uisrnn.parse_arguments()
  diarization_experiment(model_args, training_args, inference_args)

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


<commit_before>''' Provides classes to run TankCore from console environment '''
from Tank.Plugins.ConsoleOnline import RealConsoleMarkup
from tankcore import TankCore
import ConfigParser
import datetime
import fnmatch
import logging
import os
import sys
import tempfile
import time
import traceback
import signal
from optparse import OptionParser

class SingleLevelFilter(logging.Filter):
    '''Exclude or approve one msg type at a time.
    '''
    def __init__(self, passlevel, reject):
        logging.Filter.__init__(self)
        self.passlevel = passlevel
        self.reject = reject

    def filter(self, record):
        if self.reject:
            return (record.levelno != self.passlevel)
        else:
            return (record.levelno == self.passlevel)

def signal_handler(sig, frame):
    ''' required for non-tty python runs to interrupt '''
    raise KeyboardInterrupt()

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

# TODO: 2 add system resources busy check
class ConsoleTank:
    """
    Worker class that runs tank core accepting cmdline params
    """

    IGNORE_LOCKS = "ignore_locks"

    def __init__(self, options, ammofile):
        self.core = TankCore()

        self.options = options
        self.ammofile = ammofile

        self.baseconfigs_location = '/etc/yandex-tank'

        self.log_filename = self.options.log
        self.core.add_artifact_file(self.log_filename)
        self.log = logging.getLogger(__name__)

        self.signal_count = 0
        self.scheduled_start = None

    def set_baseconfigs_dir(self, directory):
        '''
        Set directory where to read configs set
        '''
        self.baseconfigs_location = directory

    def init_logging(self):
        '''
        Set up logging, as it is very important for console tool
        '''
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)

        # create file handler which logs even debug messages
        if (self.log_filename):
            file_handler = logging.FileHandler(self.log_filename)
            file_handler.setLevel(logging.DEBUG)
            file_handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(name)s %(message)s"))
            logger.addHandler(file_handler)

        # create console handler with a higher log level
        console_handler = logging.StreamHandler(sys.stdout)
        stderr_hdl = logging.StreamHandler(sys.stderr)

        fmt_verbose = logging.Formatter("%(asctime)s [%(levelname)s] %(name)s %(message)s")
        fmt_regular = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%H:%M:%S")

        if self.options.verbose:
            console_handler.setLevel(logging.DEBUG)
            console_handler.setFormatter(fmt_verbose)
            stderr_hdl.setFormatter(fmt_verbose)
        elif self.options.quiet:
            console_handler.setLevel(logging.WARNING)
            console_handler.setFormatter(fmt_regular)
            stderr_hdl.setFormatter(fmt_regular)
        else:
            console_handler.setLevel(logging.INFO)
            console_handler.setFormatter(fmt_regular)
            stderr_hdl.setFormatter(fmt_regular)

        f_err = SingleLevelFilter(logging.ERROR, True)
        f_warn = SingleLevelFilter(logging.WARNING, True)
        f_crit = SingleLevelFilter(logging.CRITICAL, True)
        console_handler.addFilter(f_err)
        console_handler.addFilter(f_warn)
        console_handler.addFilter(f_crit)
        logger.addHandler(console_handler)

        f_info = SingleLevelFilter(logging.INFO, True)
        f_debug = SingleLevelFilter(logging.DEBUG, True)
        stderr_hdl.addFilter(f_info)
        stderr_hdl.addFilter(f_debug)
        logger.addHandler(stderr_hdl)

    def __override_config_from_cmdline(self):
        ''' override config options from command line'''
        if self.options.option:
            self.core.apply_shorthand_options(self.options.option, self.MIGRATE_SECTION)

    def get_default_configs(self):
        ''' returns default configs list, from /etc and home dir '''
        configs = []
        try:
            conf_files = os.listdir(self.baseconfigs_location)
            conf_files.sort()
            for filename in conf_files:
                if fnmatch.fnmatch(filename, '*.ini'):
                    configs += [os.path.realpath(self.baseconfigs_location + os.sep + filename)]
        except OSError:
            self.log.warn(self.baseconfigs_location + ' is not acessible to get configs list')

        configs += [os.path.expanduser('~/.yandex-tank')]
        return configs

    def configure(self):
        '''         Make all console-specific preparations before running Tank        '''
        if self.options.ignore_lock:
            self.log.warn("Lock files ignored. This is highly unrecommended practice!")

        while True:
            try:
                self.core.get_lock(self.options.ignore_lock)
                break
            except Exception, exc:
                if self.options.lock_fail:
                    raise RuntimeError("Lock file present, cannot continue")
                self.log.debug("Failed to get lock: %s", traceback.format_exc(exc))
                self.log.info("Waiting 5s for retry...")
                time.sleep(5)

        try:
            configs = []

            if not self.options.no_rc:
                configs = self.get_default_configs()

            if not self.options.config:
                if os.path.exists(os.path.realpath('load.ini')):
                    self.log.info("No config passed via cmdline, using ./load.ini")
                    configs += [os.path.realpath('load.ini')]
                    self.core.add_artifact_file(os.path.realpath('load.ini'), True)
                elif os.path.exists(os.path.realpath('load.conf')):
                    # just for old 'lunapark' compatibility
                    self.log.warn("Using 'load.conf' is unrecommended, please use 'load.ini' instead")
                    conf_file = os.path.realpath('load.conf')
                    configs += [conf_file]
                    self.core.add_artifact_file(conf_file, True)
            else:
                for config_file in self.options.config:
                    self.__add_adapted_config(configs, config_file)

            self.core.load_configs(configs)

            if self.ammofile:
                self.log.debug("Ammofile: %s", self.ammofile)
                self.core.set_option(self.MIGRATE_SECTION, 'ammofile', self.ammofile[0])

            self.__override_config_from_cmdline()

            self.core.load_plugins()

            if self.options.scheduled_start:
                try:
                    self.scheduled_start = datetime.datetime.strptime(self.options.scheduled_start, '%Y-%m-%d %H:%M:%S')
                except ValueError:
                    self.scheduled_start = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d ') + self.options.scheduled_start, '%Y-%m-%d %H:%M:%S')

            if self.options.ignore_lock:
                self.core.set_option(self.core.SECTION, self.IGNORE_LOCKS, "1")

        except Exception, ex:
            self.log.info("Exception: %s", traceback.format_exc(ex))
            sys.stderr.write(RealConsoleMarkup.RED)
            self.log.error("%s", ex)
            sys.stderr.write(RealConsoleMarkup.RESET)
            sys.stderr.write(RealConsoleMarkup.TOTAL_RESET)
            self.core.release_lock()
            raise ex

    def __graceful_shutdown(self):
        ''' call shutdown routines '''
        retcode = 1
        self.log.info("Trying to shutdown gracefully...")
        retcode = self.core.plugins_end_test(retcode)
        retcode = self.core.plugins_post_process(retcode)
        self.log.info("Done graceful shutdown")
        return retcode

    def perform_test(self):
        '''
        Run the test sequence via Tank Core
        '''
        self.log.info("Performing test")
        retcode = 1
        try:
            self.core.plugins_configure()
            self.core.plugins_prepare_test()
            if self.scheduled_start:
                self.log.info("Waiting scheduled time: %s...", self.scheduled_start)
                while datetime.datetime.now() < self.scheduled_start:
                    self.log.debug("Not yet: %s < %s", datetime.datetime.now(), self.scheduled_start)
                    time.sleep(1)
                self.log.info("Time has come: %s", datetime.datetime.now())

            if self.options.manual_start:
                raw_input("Press Enter key to start test:")

            self.core.plugins_start_test()
            retcode = self.core.wait_for_finish()
            retcode = self.core.plugins_end_test(retcode)
            retcode = self.core.plugins_post_process(retcode)

        except KeyboardInterrupt as ex:
            sys.stdout.write(RealConsoleMarkup.YELLOW)
            self.log.info("Do not press Ctrl+C again, the test will be broken otherwise")
            sys.stdout.write(RealConsoleMarkup.RESET)
            sys.stdout.write(RealConsoleMarkup.TOTAL_RESET)
            self.signal_count += 1
            self.log.debug("Caught KeyboardInterrupt: %s", traceback.format_exc(ex))
            try:
                retcode = self.__graceful_shutdown()
            except KeyboardInterrupt as ex:
                self.log.debug("Caught KeyboardInterrupt again: %s", traceback.format_exc(ex))
                self.log.info("User insists on exiting, aborting graceful shutdown...")
                retcode = 1

        except Exception as ex:
            self.log.info("Exception: %s", traceback.format_exc(ex))
            sys.stderr.write(RealConsoleMarkup.RED)
            self.log.error("%s", ex)
            sys.stderr.write(RealConsoleMarkup.RESET)
            sys.stderr.write(RealConsoleMarkup.TOTAL_RESET)
            retcode = self.__graceful_shutdown()
            self.core.release_lock()

        self.log.info("Done performing test with code %s", retcode)
        return retcode

class DevNullOpts:
    log = "/dev/null"

class CompletionHelperOptionParser(OptionParser):
    def __init__(self):
        OptionParser.__init__(self, add_help_option=False)
        self.add_option('--bash-switches-list', action='store_true', dest="list_switches", help="Options list")
        self.add_option('--bash-options-prev', action='store', dest="list_options_prev", help="Options list")
        self.add_option('--bash-options-cur', action='store', dest="list_options_cur", help="Options list")

    def error(self, msg):
        pass

    def exit(self, status=0, msg=None):
        pass

    def handle_request(self, parser):
        options = self.parse_args()[0]
        if options.list_switches:
            opts = []
            for option in parser.option_list:
                if not "--bash" in option.get_opt_string():
                    opts.append(option.get_opt_string())
            print ' '.join(opts)
            exit(0)

        if options.list_options_cur or options.list_options_prev:
            cmdtank = ConsoleTank(DevNullOpts(), None)
            cmdtank.core.load_configs(cmdtank.get_default_configs())
            cmdtank.core.load_plugins()

            opts = []
            for option in cmdtank.core.get_available_options():
                opts.append(cmdtank.core.SECTION + '.' + option + '=')

            plugin_keys = cmdtank.core.config.get_options(cmdtank.core.SECTION, cmdtank.core.PLUGIN_PREFIX)
            for (plugin_name, plugin_path) in plugin_keys:
                opts.append(cmdtank.core.SECTION + '.' + cmdtank.core.PLUGIN_PREFIX + plugin_name + '=')

            for plugin in cmdtank.core.plugins.values():
                for option in plugin.get_available_options():
                    opts.append(plugin.SECTION + '.' + option + '=')
            print ' '.join(sorted(opts))
            exit(0)

<commit_msg>Remove migrate section use<commit_after>''' Provides classes to run TankCore from console environment '''
from Tank.Plugins.ConsoleOnline import RealConsoleMarkup
from tankcore import TankCore
import ConfigParser
import datetime
import fnmatch
import logging
import os
import sys
import tempfile
import time
import traceback
import signal
from optparse import OptionParser

class SingleLevelFilter(logging.Filter):
    '''Exclude or approve one msg type at a time.
    '''
    def __init__(self, passlevel, reject):
        logging.Filter.__init__(self)
        self.passlevel = passlevel
        self.reject = reject

    def filter(self, record):
        if self.reject:
            return (record.levelno != self.passlevel)
        else:
            return (record.levelno == self.passlevel)

def signal_handler(sig, frame):
    ''' required for non-tty python runs to interrupt '''
    raise KeyboardInterrupt()

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

# TODO: 2 add system resources busy check
class ConsoleTank:
    """
    Worker class that runs tank core accepting cmdline params
    """

    IGNORE_LOCKS = "ignore_locks"

    def __init__(self, options, ammofile):
        self.core = TankCore()

        self.options = options
        self.ammofile = ammofile

        self.baseconfigs_location = '/etc/yandex-tank'

        self.log_filename = self.options.log
        self.core.add_artifact_file(self.log_filename)
        self.log = logging.getLogger(__name__)

        self.signal_count = 0
        self.scheduled_start = None

    def set_baseconfigs_dir(self, directory):
        '''
        Set directory where to read configs set
        '''
        self.baseconfigs_location = directory

    def init_logging(self):
        '''
        Set up logging, as it is very important for console tool
        '''
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)

        # create file handler which logs even debug messages
        if (self.log_filename):
            file_handler = logging.FileHandler(self.log_filename)
            file_handler.setLevel(logging.DEBUG)
            file_handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(name)s %(message)s"))
            logger.addHandler(file_handler)

        # create console handler with a higher log level
        console_handler = logging.StreamHandler(sys.stdout)
        stderr_hdl = logging.StreamHandler(sys.stderr)

        fmt_verbose = logging.Formatter("%(asctime)s [%(levelname)s] %(name)s %(message)s")
        fmt_regular = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%H:%M:%S")

        if self.options.verbose:
            console_handler.setLevel(logging.DEBUG)
            console_handler.setFormatter(fmt_verbose)
            stderr_hdl.setFormatter(fmt_verbose)
        elif self.options.quiet:
            console_handler.setLevel(logging.WARNING)
            console_handler.setFormatter(fmt_regular)
            stderr_hdl.setFormatter(fmt_regular)
        else:
            console_handler.setLevel(logging.INFO)
            console_handler.setFormatter(fmt_regular)
            stderr_hdl.setFormatter(fmt_regular)

        f_err = SingleLevelFilter(logging.ERROR, True)
        f_warn = SingleLevelFilter(logging.WARNING, True)
        f_crit = SingleLevelFilter(logging.CRITICAL, True)
        console_handler.addFilter(f_err)
        console_handler.addFilter(f_warn)
        console_handler.addFilter(f_crit)
        logger.addHandler(console_handler)

        f_info = SingleLevelFilter(logging.INFO, True)
        f_debug = SingleLevelFilter(logging.DEBUG, True)
        stderr_hdl.addFilter(f_info)
        stderr_hdl.addFilter(f_debug)
        logger.addHandler(stderr_hdl)

    def __override_config_from_cmdline(self):
        ''' override config options from command line'''
        if self.options.option:
            self.core.apply_shorthand_options(self.options.option)

    def get_default_configs(self):
        ''' returns default configs list, from /etc and home dir '''
        configs = []
        try:
            conf_files = os.listdir(self.baseconfigs_location)
            conf_files.sort()
            for filename in conf_files:
                if fnmatch.fnmatch(filename, '*.ini'):
                    configs += [os.path.realpath(self.baseconfigs_location + os.sep + filename)]
        except OSError:
            self.log.warn(self.baseconfigs_location + ' is not acessible to get configs list')

        configs += [os.path.expanduser('~/.yandex-tank')]
        return configs

    def configure(self):
        '''         Make all console-specific preparations before running Tank        '''
        if self.options.ignore_lock:
            self.log.warn("Lock files ignored. This is highly unrecommended practice!")

        while True:
            try:
                self.core.get_lock(self.options.ignore_lock)
                break
            except Exception, exc:
                if self.options.lock_fail:
                    raise RuntimeError("Lock file present, cannot continue")
                self.log.debug("Failed to get lock: %s", traceback.format_exc(exc))
                self.log.info("Waiting 5s for retry...")
                time.sleep(5)

        try:
            configs = []

            if not self.options.no_rc:
                configs = self.get_default_configs()

            if not self.options.config:
                if os.path.exists(os.path.realpath('load.ini')):
                    self.log.info("No config passed via cmdline, using ./load.ini")
                    configs += [os.path.realpath('load.ini')]
                    self.core.add_artifact_file(os.path.realpath('load.ini'), True)
                elif os.path.exists(os.path.realpath('load.conf')):
                    # just for old 'lunapark' compatibility
                    self.log.warn("Using 'load.conf' is unrecommended, please use 'load.ini' instead")
                    conf_file = os.path.realpath('load.conf')
                    configs += [conf_file]
                    self.core.add_artifact_file(conf_file, True)
            else:
                for config_file in self.options.config:
                    self.__add_adapted_config(configs, config_file)

            self.core.load_configs(configs)

            if self.ammofile:
                self.log.debug("Ammofile: %s", self.ammofile)
                self.core.set_option("phantom", 'ammofile', self.ammofile[0])

            self.__override_config_from_cmdline()

            self.core.load_plugins()

            if self.options.scheduled_start:
                try:
                    self.scheduled_start = datetime.datetime.strptime(self.options.scheduled_start, '%Y-%m-%d %H:%M:%S')
                except ValueError:
                    self.scheduled_start = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d ') + self.options.scheduled_start, '%Y-%m-%d %H:%M:%S')

            if self.options.ignore_lock:
                self.core.set_option(self.core.SECTION, self.IGNORE_LOCKS, "1")

        except Exception, ex:
            self.log.info("Exception: %s", traceback.format_exc(ex))
            sys.stderr.write(RealConsoleMarkup.RED)
            self.log.error("%s", ex)
            sys.stderr.write(RealConsoleMarkup.RESET)
            sys.stderr.write(RealConsoleMarkup.TOTAL_RESET)
            self.core.release_lock()
            raise ex

    def __graceful_shutdown(self):
        ''' call shutdown routines '''
        retcode = 1
        self.log.info("Trying to shutdown gracefully...")
        retcode = self.core.plugins_end_test(retcode)
        retcode = self.core.plugins_post_process(retcode)
        self.log.info("Done graceful shutdown")
        return retcode

    def perform_test(self):
        '''
        Run the test sequence via Tank Core
        '''
        self.log.info("Performing test")
        retcode = 1
        try:
            self.core.plugins_configure()
            self.core.plugins_prepare_test()
            if self.scheduled_start:
                self.log.info("Waiting scheduled time: %s...", self.scheduled_start)
                while datetime.datetime.now() < self.scheduled_start:
                    self.log.debug("Not yet: %s < %s", datetime.datetime.now(), self.scheduled_start)
                    time.sleep(1)
                self.log.info("Time has come: %s", datetime.datetime.now())

            if self.options.manual_start:
                raw_input("Press Enter key to start test:")

            self.core.plugins_start_test()
            retcode = self.core.wait_for_finish()
            retcode = self.core.plugins_end_test(retcode)
            retcode = self.core.plugins_post_process(retcode)

        except KeyboardInterrupt as ex:
            sys.stdout.write(RealConsoleMarkup.YELLOW)
            self.log.info("Do not press Ctrl+C again, the test will be broken otherwise")
            sys.stdout.write(RealConsoleMarkup.RESET)
            sys.stdout.write(RealConsoleMarkup.TOTAL_RESET)
            self.signal_count += 1
            self.log.debug("Caught KeyboardInterrupt: %s", traceback.format_exc(ex))
            try:
                retcode = self.__graceful_shutdown()
            except KeyboardInterrupt as ex:
                self.log.debug("Caught KeyboardInterrupt again: %s", traceback.format_exc(ex))
                self.log.info("User insists on exiting, aborting graceful shutdown...")
                retcode = 1

        except Exception as ex:
            self.log.info("Exception: %s", traceback.format_exc(ex))
            sys.stderr.write(RealConsoleMarkup.RED)
            self.log.error("%s", ex)
            sys.stderr.write(RealConsoleMarkup.RESET)
            sys.stderr.write(RealConsoleMarkup.TOTAL_RESET)
            retcode = self.__graceful_shutdown()
            self.core.release_lock()

        self.log.info("Done performing test with code %s", retcode)
        return retcode

class DevNullOpts:
    log = "/dev/null"

class CompletionHelperOptionParser(OptionParser):
    def __init__(self):
        OptionParser.__init__(self, add_help_option=False)
        self.add_option('--bash-switches-list', action='store_true', dest="list_switches", help="Options list")
        self.add_option('--bash-options-prev', action='store', dest="list_options_prev", help="Options list")
        self.add_option('--bash-options-cur', action='store', dest="list_options_cur", help="Options list")

    def error(self, msg):
        pass

    def exit(self, status=0, msg=None):
        pass

    def handle_request(self, parser):
        options = self.parse_args()[0]
        if options.list_switches:
            opts = []
            for option in parser.option_list:
                if not "--bash" in option.get_opt_string():
                    opts.append(option.get_opt_string())
            print ' '.join(opts)
            exit(0)

        if options.list_options_cur or options.list_options_prev:
            cmdtank = ConsoleTank(DevNullOpts(), None)
            cmdtank.core.load_configs(cmdtank.get_default_configs())
            cmdtank.core.load_plugins()

            opts = []
            for option in cmdtank.core.get_available_options():
                opts.append(cmdtank.core.SECTION + '.' + option + '=')

            plugin_keys = cmdtank.core.config.get_options(cmdtank.core.SECTION, cmdtank.core.PLUGIN_PREFIX)
            for (plugin_name, plugin_path) in plugin_keys:
                opts.append(cmdtank.core.SECTION + '.' + cmdtank.core.PLUGIN_PREFIX + plugin_name + '=')

            for plugin in cmdtank.core.plugins.values():
                for option in plugin.get_available_options():
                    opts.append(plugin.SECTION + '.' + option + '=')
            print ' '.join(sorted(opts))
            exit(0)

<|endoftext|>


<commit_before>
from test_project.settings import *

DATABASES['mongo'] = {
  'ENGINE' : 'django_mongodb_engine',
  'NAME' : 'mutant',
  'OPTIONS': {
    'OPERATIONS': {
      'save' : {'safe' : True},
    }
  }
}

SOUTH_DATABASE_ADAPTERS = {'mongo': 'django_mongodb_engine.south'}

INSTALLED_APPS.extend(['django_mongodb_engine', 'djangotoolbox'])

# FK and M2M are not supported for nonrel db so we make sure to avoid
# loading mutant.contrib.related
INSTALLED_APPS.remove('mutant.contrib.related')
# But we can test the non rel fields
INSTALLED_APPS.append('mutant.contrib.nonrel')

DATABASE_ROUTERS = (
    'mongodb_router.MongoRouter',
)
<commit_msg>Make sure to load the new mongo south adapter<commit_after>
from test_project.settings import *

DATABASES['mongo'] = {
  'ENGINE' : 'django_mongodb_engine',
  'NAME' : 'mutant',
  'OPTIONS': {
    'OPERATIONS': {
      'save' : {'safe' : True},
    }
  }
}

SOUTH_DATABASE_ADAPTERS = {'mongo': 'django_mongodb_engine.south_adapter'}

INSTALLED_APPS.extend(['django_mongodb_engine', 'djangotoolbox'])

# FK and M2M are not supported for nonrel db so we make sure to avoid
# loading mutant.contrib.related
INSTALLED_APPS.remove('mutant.contrib.related')
# But we can test the non rel fields
INSTALLED_APPS.append('mutant.contrib.nonrel')

DATABASE_ROUTERS = (
    'mongodb_router.MongoRouter',
)
<|endoftext|>


<commit_before>import numpy as np
from PySide import QtGui, QtCore
import sharppy.sharptab as tab
from scipy.misc import bytescale
from sharppy.sharptab.constants import *

__all__ = ['backgroundSlinky', 'plotSlinky']

class backgroundSlinky(QtGui.QFrame):
    '''
    Draw the background frame and lines for the Storm Slinky plot frame
    '''
    def __init__(self):
        super(backgroundSlinky, self).__init__()
        self.initUI()

    def initUI(self):
        ## window configuration settings,
        ## sich as padding, width, height, and
        ## min/max plot axes
        self.lpad = 0; self.rpad = 0
        self.tpad = 0; self.bpad = 20
        self.wid = self.size().width() - self.rpad
        self.hgt = self.size().height() - self.bpad
        self.tlx = self.rpad; self.tly = self.tpad
        self.brx = self.wid; self.bry = self.hgt
        self.center_hodo()
        self.title_font = QtGui.QFont('Helvetica', 9)
        self.plot_font = QtGui.QFont('Helvetica', 9)
        self.title_metrics = QtGui.QFontMetrics( self.title_font )
        self.plot_metrics = QtGui.QFontMetrics( self.plot_font )
        self.title_height = self.title_metrics.height()
        self.plot_height = self.plot_metrics.height()
        self.plotBitMap = QtGui.QPixmap(self.width(), self.height())
        self.plotBitMap.fill(QtCore.Qt.black)
        self.plotBackground()

    def center_hodo(self):
        '''
        Center the hodograph in the window. Can/Should be overwritten.

        '''
        self.centerx = self.wid / 2; self.centery = self.hgt / 2
        self.hodomag = 6000.*1.7
        self.scale = (self.brx - self.tlx) / self.hodomag

    def resizeEvent(self, e):
        '''
        Handles the event the window is resized
        '''
        self.initUI()

    def draw_frame(self, qp):
        '''
        Draw the background frame.
        qp: QtGui.QPainter object
        '''
        ## set a new pen to draw with
        pen = QtGui.QPen(QtCore.Qt.white, 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        qp.setFont(self.title_font)

        ## draw the borders in white
        qp.drawLine(self.tlx, self.tly, self.brx, self.tly)
        qp.drawLine(self.brx, self.tly, self.brx, self.bry)
        qp.drawLine(self.brx, self.bry, self.tlx, self.bry)
        qp.drawLine(self.tlx, self.bry, self.tlx, self.tly)

        rect0 = QtCore.QRect(2, self.bry-11, 11, self.title_height)
        qp.drawText(rect0, QtCore.Qt.TextDontClip | QtCore.Qt.AlignLeft, 'Storm Slinky')

    def draw_axes(self, qp):
        '''
        Draw the X, Y Axes.
        --------
        qp: QtGui.QPainter object

        '''
        ## initialize a white pen to draw the frame axes
        pen = QtGui.QPen(QtGui.QColor('#003366'), 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        ## draw the frame axes
        qp.drawLine(self.centerx, self.tly, self.centerx, self.bry)
        qp.drawLine(self.tlx, self.centery, self.brx, self.centery)

    def plotBackground(self):
        qp = QtGui.QPainter()
        qp.begin(self.plotBitMap)
        qp.setRenderHint(qp.Antialiasing)
        qp.setRenderHint(qp.TextAntialiasing)
        ## draw the frame
        self.draw_frame(qp)
        self.draw_axes(qp)
        qp.end()

    def xy_to_pix(self, x, y):
        '''
        Function to convert (x, y) to pixel (x, y) coordinates.
        --------
        x: the x distance component
        y: the y distance component

        '''
        xx = self.centerx + (x * self.scale)
        yy = self.centery - (y * self.scale)
        return xx, yy

class plotSlinky(backgroundSlinky):
    '''
    Plot the data on the frame. Inherits the background class that
    plots the frame.
    '''
    def __init__(self, prof):
        self.prof = prof
        super(plotSlinky, self).__init__()
        self.slinky_traj = self.prof.slinky_traj
        self.updraft_tilt = self.prof.updraft_tilt
        self.smu = self.prof.srwind[0]
        self.smv = self.prof.srwind[1]

    def resizeEvent(self, e):
        '''
        Handles when the window is resized
        '''
        super(plotSlinky, self).resizeEvent(e)
        self.plotData()

    def paintEvent(self, e):
        '''
        Handles painting on the frame
        '''
        ## this function handles painting the plot
        super(plotSlinky, self).paintEvent(e)
        ## create a new painter obkect
        qp = QtGui.QPainter()
        qp.begin(self)
        ## end the painter
        qp.drawPixmap(0,0,self.plotBitMap)
        qp.end()

    def plotData(self):
        qp = QtGui.QPainter()
        qp.begin(self.plotBitMap)
        qp.setRenderHint(qp.Antialiasing)
        qp.setRenderHint(qp.TextAntialiasing)
        self.plotSlinky(qp)
        self.plotSMV(qp)
        self.plotTilt(qp)
        qp.end()

    def plotSMV(self, qp):
        pen = QtGui.QPen(QtGui.QColor("#FFFFFF"), 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        wdir, wspd = tab.utils.comp2vec(self.smu, self.smv)
        u, v = tab.utils.vec2comp(wdir, 3000)
        motion_x, motion_y = self.xy_to_pix(u,v)
        center_x, center_y = self.xy_to_pix(0,0)
        qp.drawLine(motion_x,motion_y, center_x,center_y)

    def plotTilt(self, qp):
        pen = QtGui.QPen(QtGui.QColor("#FFFFFF"), 1, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        qp.setFont(self.title_font)
        rect0 = QtCore.QRect(self.brx-30, self.tly+2, 30, self.title_height)
        qp.drawText(rect0, QtCore.Qt.TextDontClip | QtCore.Qt.AlignRight, str(round(self.updraft_tilt,0)) + ' deg  ')

    def plotSlinky(self, qp):
        low_level_color = QtGui.QColor("#FF0000")
        mid_level_color = QtGui.QColor("#00FF00")
        upper_level_color = QtGui.QColor("#FFFF00")
        trop_level_color = QtGui.QColor("#00FFFF")

        pen = QtGui.QPen(trop_level_color, 1, QtCore.Qt.SolidLine)
        for i in self.slinky_traj[::-1]:
            x = i[0]
            y = i[1]
            z = i[2]
            if z < 3000:
                pen = QtGui.QPen(low_level_color, 1, QtCore.Qt.SolidLine)
            elif z < 6000:
                pen = QtGui.QPen(mid_level_color, 1, QtCore.Qt.SolidLine)
            elif z < 9000:
                pen = QtGui.QPen(upper_level_color, 1, QtCore.Qt.SolidLine)
            elif z < 12000:
                pen = QtGui.QPen(trop_level_color, 1, QtCore.Qt.SolidLine)
            elif z == self.slinky_traj[-1][2]:
                pen = QtGui.QPen(QtGui.QColor("#FF00FF"), 1, QtCore.Qt.SolidLine)
            else:
                continue

            qp.setPen(pen)
            xx, yy = self.xy_to_pix(x,y)
            center = QtCore.QPointF(xx, yy)
            qp.drawEllipse(center, 6, 6)

<commit_msg>updated EL condition<commit_after>import numpy as np
from PySide import QtGui, QtCore
import sharppy.sharptab as tab
from scipy.misc import bytescale
from sharppy.sharptab.constants import *

__all__ = ['backgroundSlinky', 'plotSlinky']

class backgroundSlinky(QtGui.QFrame):
    '''
    Draw the background frame and lines for the Storm Slinky plot frame
    '''
    def __init__(self):
        super(backgroundSlinky, self).__init__()
        self.initUI()

    def initUI(self):
        ## window configuration settings,
        ## sich as padding, width, height, and
        ## min/max plot axes
        self.lpad = 0; self.rpad = 0
        self.tpad = 0; self.bpad = 20
        self.wid = self.size().width() - self.rpad
        self.hgt = self.size().height() - self.bpad
        self.tlx = self.rpad; self.tly = self.tpad
        self.brx = self.wid; self.bry = self.hgt
        self.center_hodo()
        self.title_font = QtGui.QFont('Helvetica', 9)
        self.plot_font = QtGui.QFont('Helvetica', 9)
        self.title_metrics = QtGui.QFontMetrics( self.title_font )
        self.plot_metrics = QtGui.QFontMetrics( self.plot_font )
        self.title_height = self.title_metrics.height()
        self.plot_height = self.plot_metrics.height()
        self.plotBitMap = QtGui.QPixmap(self.width(), self.height())
        self.plotBitMap.fill(QtCore.Qt.black)
        self.plotBackground()

    def center_hodo(self):
        '''
        Center the hodograph in the window. Can/Should be overwritten.

        '''
        self.centerx = self.wid / 2; self.centery = self.hgt / 2
        self.hodomag = 6000.*1.7
        self.scale = (self.brx - self.tlx) / self.hodomag

    def resizeEvent(self, e):
        '''
        Handles the event the window is resized
        '''
        self.initUI()

    def draw_frame(self, qp):
        '''
        Draw the background frame.
        qp: QtGui.QPainter object
        '''
        ## set a new pen to draw with
        pen = QtGui.QPen(QtCore.Qt.white, 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        qp.setFont(self.title_font)

        ## draw the borders in white
        qp.drawLine(self.tlx, self.tly, self.brx, self.tly)
        qp.drawLine(self.brx, self.tly, self.brx, self.bry)
        qp.drawLine(self.brx, self.bry, self.tlx, self.bry)
        qp.drawLine(self.tlx, self.bry, self.tlx, self.tly)

        rect0 = QtCore.QRect(2, self.bry-11, 11, self.title_height)
        qp.drawText(rect0, QtCore.Qt.TextDontClip | QtCore.Qt.AlignLeft, 'Storm Slinky')

    def draw_axes(self, qp):
        '''
        Draw the X, Y Axes.
        --------
        qp: QtGui.QPainter object

        '''
        ## initialize a white pen to draw the frame axes
        pen = QtGui.QPen(QtGui.QColor('#003366'), 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        ## draw the frame axes
        qp.drawLine(self.centerx, self.tly, self.centerx, self.bry)
        qp.drawLine(self.tlx, self.centery, self.brx, self.centery)

    def plotBackground(self):
        qp = QtGui.QPainter()
        qp.begin(self.plotBitMap)
        qp.setRenderHint(qp.Antialiasing)
        qp.setRenderHint(qp.TextAntialiasing)
        ## draw the frame
        self.draw_frame(qp)
        self.draw_axes(qp)
        qp.end()

    def xy_to_pix(self, x, y):
        '''
        Function to convert (x, y) to pixel (x, y) coordinates.
        --------
        x: the x distance component
        y: the y distance component

        '''
        xx = self.centerx + (x * self.scale)
        yy = self.centery - (y * self.scale)
        return xx, yy

class plotSlinky(backgroundSlinky):
    '''
    Plot the data on the frame. Inherits the background class that
    plots the frame.
    '''
    def __init__(self, prof):
        self.prof = prof
        super(plotSlinky, self).__init__()
        self.slinky_traj = self.prof.slinky_traj
        self.updraft_tilt = self.prof.updraft_tilt
        self.smu = self.prof.srwind[0]
        self.smv = self.prof.srwind[1]

    def resizeEvent(self, e):
        '''
        Handles when the window is resized
        '''
        super(plotSlinky, self).resizeEvent(e)
        self.plotData()

    def paintEvent(self, e):
        '''
        Handles painting on the frame
        '''
        ## this function handles painting the plot
        super(plotSlinky, self).paintEvent(e)
        ## create a new painter obkect
        qp = QtGui.QPainter()
        qp.begin(self)
        ## end the painter
        qp.drawPixmap(0,0,self.plotBitMap)
        qp.end()

    def plotData(self):
        qp = QtGui.QPainter()
        qp.begin(self.plotBitMap)
        qp.setRenderHint(qp.Antialiasing)
        qp.setRenderHint(qp.TextAntialiasing)
        self.plotSlinky(qp)
        self.plotSMV(qp)
        self.plotTilt(qp)
        qp.end()

    def plotSMV(self, qp):
        pen = QtGui.QPen(QtGui.QColor("#FFFFFF"), 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        wdir, wspd = tab.utils.comp2vec(self.smu, self.smv)
        u, v = tab.utils.vec2comp(wdir, 3000)
        motion_x, motion_y = self.xy_to_pix(u,v)
        center_x, center_y = self.xy_to_pix(0,0)
        qp.drawLine(motion_x,motion_y, center_x,center_y)

    def plotTilt(self, qp):
        pen = QtGui.QPen(QtGui.QColor("#FFFFFF"), 1, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        qp.setFont(self.title_font)
        rect0 = QtCore.QRect(self.brx-30, self.tly+2, 30, self.title_height)
        qp.drawText(rect0, QtCore.Qt.TextDontClip | QtCore.Qt.AlignRight, str(round(self.updraft_tilt,0)) + ' deg  ')

    def plotSlinky(self, qp):
        low_level_color = QtGui.QColor("#FF0000")
        mid_level_color = QtGui.QColor("#00FF00")
        upper_level_color = QtGui.QColor("#FFFF00")
        trop_level_color = QtGui.QColor("#00FFFF")

        pen = QtGui.QPen(trop_level_color, 1, QtCore.Qt.SolidLine)
        for i in self.slinky_traj[::-1]:
            x = i[0]
            y = i[1]
            z = i[2]
            if z == self.slinky_traj[-1][2]:
                pen = QtGui.QPen(QtGui.QColor("#FF00FF"), 1, QtCore.Qt.SolidLine)
            elif z < 3000:
                pen = QtGui.QPen(low_level_color, 1, QtCore.Qt.SolidLine)
            elif z < 6000:
                pen = QtGui.QPen(mid_level_color, 1, QtCore.Qt.SolidLine)
            elif z < 9000:
                pen = QtGui.QPen(upper_level_color, 1, QtCore.Qt.SolidLine)
            elif z < 12000:
                pen = QtGui.QPen(trop_level_color, 1, QtCore.Qt.SolidLine)
            else:
                continue
            qp.setPen(pen)
            xx, yy = self.xy_to_pix(x,y)
            center = QtCore.QPointF(xx, yy)
            qp.drawEllipse(center, 6, 6)

<|endoftext|>


<commit_before>import compiler
import re

def is_log_callfunc(n):
    """LOG.xxx('hello %s' % xyz) and LOG('hello')"""
    if isinstance(n.parent, compiler.ast.Mod):
        n = n.parent
    if isinstance(n.parent, compiler.ast.CallFunc):
        if isinstance(n.parent.node, compiler.ast.Getattr):
            if isinstance(n.parent.node.getChildNodes()[0],
                          compiler.ast.Name):
                if n.parent.node.getChildNodes()[0].name == 'LOG':
                    return True
    return False

def is_log_i18n_msg_with_mod(n):
    """LOG.xxx("Hello %s" % xyz) should be LOG.xxx("Hello %s", xyz)"""
    if not isinstance(n.parent.parent, compiler.ast.Mod):
        return False
    n = n.parent.parent
    if isinstance(n.parent, compiler.ast.CallFunc):
        if isinstance(n.parent.node, compiler.ast.Getattr):
            if isinstance(n.parent.node.getChildNodes()[0],
                          compiler.ast.Name):
                if n.parent.node.getChildNodes()[0].name == 'LOG':
                    return True
    return False

def is_wrong_i18n_format(n):
    """Check _('hello %s' % xyz)"""
    if isinstance(n.parent, compiler.ast.Mod):
        n = n.parent
    if isinstance(n.parent, compiler.ast.CallFunc):
        if isinstance(n.parent.node, compiler.ast.Name):
            if n.parent.node.name == '_':
                return True
    return False

"""
Used for check message need be localized or not.
(predicate_func, action, message)
"""
i18n_msg_predicates = [
    # Skip ['hello world', 1]
    (lambda n: isinstance(n.parent, compiler.ast.List), 'skip', ''),
    # Skip {'hellow world', 1}
    (lambda n: isinstance(n.parent, compiler.ast.Dict), 'skip', ''),
    # Skip msg['hello world']
    (lambda n: isinstance(n.parent, compiler.ast.Subscript), 'skip', ''),
    # Skip doc string
    (lambda n: isinstance(n.parent, compiler.ast.Discard), 'skip', ''),
    # Skip msg = "hello", in normal, message should more than one word
    (lambda n: len(n.value.strip().split(' ')) <= 1, 'skip', ''),
    # Skip msg = 'hello world' + vars + 'world hello'
    (lambda n: isinstance(n.parent, compiler.ast.Add), 'skip', ''),
    # Skip xml markers msg = "<test></test>"
    (lambda n: len(re.compile("</.*>").findall(n.value)) > 0, 'skip', ''),
    # Skip sql statement
    (lambda n: len(
        re.compile("^SELECT.*FROM", flags=re.I).findall(n.value)) > 0,
     'skip', ''),
    # LOG.xxx()
    (is_log_callfunc, 'error', 'Message must be localized'),
    # _('hello %s' % xyz) should be _('hello %s') % xyz
    (is_wrong_i18n_format, 'error',
     ("Message format was wrong, _('hello %s' % xyz) "
      "should be _('hello %s') % xyz")),
    # default
    (lambda n: True, 'warn', 'Message might need localized')
]

"""
Used for checking message format. (checker_func, message)
"""
msg_format_checkers = [
    # If message contain more than on format specifier, it should use
    # mapping key
    (lambda n: len(re.compile("%[bcdeEfFgGnosxX]").findall(n.value)) > 1,
     "The message shouldn't contain more than one format specifier"),
    # Check capital
    (lambda n: n.value.split(' ')[0].count('_') == 0 and
     n.value[0].isalpha() and
     n.value[0].islower(),
     "First letter must be capital"),
    (is_log_i18n_msg_with_mod,
     'LOG.xxx("Hello %s" % xyz) should be LOG.xxx("Hello %s", xyz)')
]

file_black_list = ["./neutron/tests/unit",
                   "./neutron/openstack",
                   "./quantum/plugins/bigswitch/tests"]
<commit_msg>Fix message i18n error<commit_after>import compiler
import re

def is_log_callfunc(n):
    """LOG.xxx('hello %s' % xyz) and LOG('hello')"""
    if isinstance(n.parent, compiler.ast.Mod):
        n = n.parent
    if isinstance(n.parent, compiler.ast.CallFunc):
        if isinstance(n.parent.node, compiler.ast.Getattr):
            if isinstance(n.parent.node.getChildNodes()[0],
                          compiler.ast.Name):
                if n.parent.node.getChildNodes()[0].name == 'LOG':
                    return True
    return False

def is_log_i18n_msg_with_mod(n):
    """LOG.xxx("Hello %s" % xyz) should be LOG.xxx("Hello %s", xyz)"""
    if not isinstance(n.parent.parent, compiler.ast.Mod):
        return False
    n = n.parent.parent
    if isinstance(n.parent, compiler.ast.CallFunc):
        if isinstance(n.parent.node, compiler.ast.Getattr):
            if isinstance(n.parent.node.getChildNodes()[0],
                          compiler.ast.Name):
                if n.parent.node.getChildNodes()[0].name == 'LOG':
                    return True
    return False

def is_wrong_i18n_format(n):
    """Check _('hello %s' % xyz)"""
    if isinstance(n.parent, compiler.ast.Mod):
        n = n.parent
    if isinstance(n.parent, compiler.ast.CallFunc):
        if isinstance(n.parent.node, compiler.ast.Name):
            if n.parent.node.name == '_':
                return True
    return False

"""
Used for check message need be localized or not.
(predicate_func, action, message)
"""
i18n_msg_predicates = [
    # Skip ['hello world', 1]
    (lambda n: isinstance(n.parent, compiler.ast.List), 'skip', ''),
    # Skip {'hellow world', 1}
    (lambda n: isinstance(n.parent, compiler.ast.Dict), 'skip', ''),
    # Skip msg['hello world']
    (lambda n: isinstance(n.parent, compiler.ast.Subscript), 'skip', ''),
    # Skip doc string
    (lambda n: isinstance(n.parent, compiler.ast.Discard), 'skip', ''),
    # Skip msg = "hello", in normal, message should more than one word
    (lambda n: len(n.value.strip().split(' ')) <= 1, 'skip', ''),
    # Skip msg = 'hello world' + vars + 'world hello'
    (lambda n: isinstance(n.parent, compiler.ast.Add), 'skip', ''),
    # Skip xml markers msg = "<test></test>"
    (lambda n: len(re.compile("</.*>").findall(n.value)) > 0, 'skip', ''),
    # Skip sql statement
    (lambda n: len(
        re.compile("^SELECT.*FROM", flags=re.I).findall(n.value)) > 0,
     'skip', ''),
    # LOG.xxx()
    (is_log_callfunc, 'error', 'Message must be localized'),
    # _('hello %s' % xyz) should be _('hello %s') % xyz
    (is_wrong_i18n_format, 'error',
     ("Message format was wrong, _('hello %s' % xyz) "
      "should be _('hello %s') % xyz")),
    # default
    (lambda n: True, 'warn', 'Message might need localized')
]

"""
Used for checking message format. (checker_func, message)
"""
msg_format_checkers = [
    # If message contain more than on format specifier, it should use
    # mapping key
    (lambda n: len(re.compile("%[bcdeEfFgGnosxX]").findall(n.value)) > 1,
     "The message shouldn't contain more than one format specifier"),
    # Check capital
    (lambda n: n.value.split(' ')[0].count('_') == 0 and
     n.value[0].isalpha() and
     n.value[0].islower(),
     "First letter must be capital"),
    (is_log_i18n_msg_with_mod,
     'LOG.xxx("Hello %s" % xyz) should be LOG.xxx("Hello %s", xyz)')
]

file_black_list = ["./neutron/tests/unit",
                   "./neutron/openstack",
                   "./neutron/plugins/bigswitch/tests"]
<|endoftext|>