<commit_before>#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import time

import pyauto_functional
import pyauto

class NetflixTest(pyauto.PyUITest):
  """Test case for Netflix player"""

  # Netflix player states
  _is_playing = '4'

  _title_homepage = 'http://movies.netflix.com/WiHome'
  _signout_page = 'https://account.netflix.com/Logout'
  # 30 Rock
  _test_title = 'http://movies.netflix.com/WiPlayer?'+ \
               'movieid=70136124&trkid=2361637&t=30+Rock'

  def tearDown(self):
    self._SignOut()
    pyauto.PyUITest.tearDown(self)

  def _IsNetflixPluginEnabled(self):
    """Determine Netflix plugin availability and its state"""
    return [x for x in self.GetPluginsInfo().Plugins() \
               if x['name'] == 'Netflix' and x['enabled']]

  def _LoginToNetflix(self):
    """Login to Netflix"""
    credentials = self.GetPrivateInfo()['test_netflix_acct']
    self.NavigateToURL(credentials['login_url'])
    login_js = """
        document.getElementById('email').value='%s';
        document.getElementById('password').value='%s';
        window.domAutomationController.send('ok');
    """ % (credentials['username'], credentials['password'])
    self.assertEqual(self.ExecuteJavascript(login_js), 'ok',
        msg='Failed to set login credentials')
    self.assertTrue(self.SubmitForm('login-form'),
        msg='Login to Netflix failed')

  def _HandleInfobars(self):
    """Manage infobars, come up during the test.

    We expect password and Netflix infobars. Processing only Netflix infobar,
    since to start a vidoe, pressing the OK button is a must. We can keep other
    inforbars open."""
    self.WaitForInfobarCount(2)
    tab_info = self.GetBrowserInfo()['windows'][0]['tabs'][0]
    infobars = tab_info['infobars']
    index = 0
    netflix_infobar_status = False
    for infobar in infobars:
       if infobar['buttons'][0] == 'OK':
         self.PerformActionOnInfobar('accept', infobar_index=index)
         netflix_infobar_status = True
       index = index + 1
    self.assertTrue(netflix_infobar_status,
                    msg='Netflix infobar did not show up')

  def _CurrentPlaybackTime(self):
    """Returns the current playback time in seconds"""
    time = self.ExecuteJavascript("""
        time = nrdp.video.currentTime;
        window.domAutomationController.send(time + '');
    """)
    return int(float(time))

  def _SignOut(self):
    """Sing out from Netflix Login"""
    self.NavigateToURL(self._signout_page)

  def _LoginAndStartPlaying(self):
    """Login and start playing the video"""
    self.assertTrue(self._IsNetflixPluginEnabled(),
                    msg='Netflix plugin is disabled or not available')
    self._LoginToNetflix()
    self.assertTrue(self.WaitUntil(
        lambda:self.GetActiveTabURL().spec(),
        expect_retval=self._title_homepage),
        msg='Login to Netflix failed')
    self.NavigateToURL(self._test_title)
    self._HandleInfobars()
    self.assertTrue(self.WaitUntil(
        lambda: self.ExecuteJavascript("""
            player_status = nrdp.video.readyState;
            window.domAutomationController.send(player_status + '');
        """), expect_retval=self._is_playing),
        msg='Player did not start playing the title')

  def testPlayerLoadsAndPlays(self):
    """Test that Netflix player loads and plays the title"""
    self._LoginAndStartPlaying()

  def testPlaying(self):
    """Test that title playing progresses"""
    self._LoginAndStartPlaying()
    title_length =  self.ExecuteJavascript("""
        time = nrdp.video.duration;
        window.domAutomationController.send(time + '');
    """)
    title_length = int(float(title_length))
    prev_time = 0
    current_time = 0
    count = 0
    while current_time < title_length:
      # We want to test playing only for five seconds
      count = count + 1
      if count == 5:
        break
      current_time = self._CurrentPlaybackTime()
      self.assertTrue(prev_time <= current_time,
          msg='Prev playing time %s is greater than current time %s'
          % (prev_time, current_time))
      prev_time = current_time
      # play video for some time
      time.sleep(1)

if __name__ == '__main__':
  pyauto_functional.Main()
<commit_msg>In case player doesn't start playing at all, current_time =0 value still clears the test. So adding a recheck to make sire that Netflix player did play something in last 10 seconds. Review URL: http://codereview.chromium.org/8375043<commit_after>#!/usr/bin/python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import time

import pyauto_functional
import pyauto

class NetflixTest(pyauto.PyUITest):
  """Test case for Netflix player"""

  # Netflix player states
  _is_playing = '4'

  _title_homepage = 'http://movies.netflix.com/WiHome'
  _signout_page = 'https://account.netflix.com/Logout'
  # 30 Rock
  _test_title = 'http://movies.netflix.com/WiPlayer?'+ \
               'movieid=70136124&trkid=2361637&t=30+Rock'

  def tearDown(self):
    self._SignOut()
    pyauto.PyUITest.tearDown(self)

  def _IsNetflixPluginEnabled(self):
    """Determine Netflix plugin availability and its state"""
    return [x for x in self.GetPluginsInfo().Plugins() \
               if x['name'] == 'Netflix' and x['enabled']]

  def _LoginToNetflix(self):
    """Login to Netflix"""
    credentials = self.GetPrivateInfo()['test_netflix_acct']
    self.NavigateToURL(credentials['login_url'])
    login_js = """
        document.getElementById('email').value='%s';
        document.getElementById('password').value='%s';
        window.domAutomationController.send('ok');
    """ % (credentials['username'], credentials['password'])
    self.assertEqual(self.ExecuteJavascript(login_js), 'ok',
        msg='Failed to set login credentials')
    self.assertTrue(self.SubmitForm('login-form'),
        msg='Login to Netflix failed')

  def _HandleInfobars(self):
    """Manage infobars, come up during the test.

    We expect password and Netflix infobars. Processing only Netflix infobar,
    since to start a vidoe, pressing the OK button is a must. We can keep other
    inforbars open."""
    self.WaitForInfobarCount(2)
    tab_info = self.GetBrowserInfo()['windows'][0]['tabs'][0]
    infobars = tab_info['infobars']
    index = 0
    netflix_infobar_status = False
    for infobar in infobars:
       if infobar['buttons'][0] == 'OK':
         self.PerformActionOnInfobar('accept', infobar_index=index)
         netflix_infobar_status = True
       index = index + 1
    self.assertTrue(netflix_infobar_status,
                    msg='Netflix infobar did not show up')

  def _CurrentPlaybackTime(self):
    """Returns the current playback time in seconds"""
    time = self.ExecuteJavascript("""
        time = nrdp.video.currentTime;
        window.domAutomationController.send(time + '');
    """)
    return int(float(time))

  def _SignOut(self):
    """Sing out from Netflix Login"""
    self.NavigateToURL(self._signout_page)

  def _LoginAndStartPlaying(self):
    """Login and start playing the video"""
    self.assertTrue(self._IsNetflixPluginEnabled(),
                    msg='Netflix plugin is disabled or not available')
    self._LoginToNetflix()
    self.assertTrue(self.WaitUntil(
        lambda:self.GetActiveTabURL().spec(),
        expect_retval=self._title_homepage),
        msg='Login to Netflix failed')
    self.NavigateToURL(self._test_title)
    self._HandleInfobars()
    self.assertTrue(self.WaitUntil(
        lambda: self.ExecuteJavascript("""
            player_status = nrdp.video.readyState;
            window.domAutomationController.send(player_status + '');
        """), expect_retval=self._is_playing),
        msg='Player did not start playing the title')

  def testPlayerLoadsAndPlays(self):
    """Test that Netflix player loads and plays the title"""
    self._LoginAndStartPlaying()

  def testPlaying(self):
    """Test that title playing progresses"""
    self._LoginAndStartPlaying()
    title_length =  self.ExecuteJavascript("""
        time = nrdp.video.duration;
        window.domAutomationController.send(time + '');
    """)
    title_length = int(float(title_length))
    prev_time = 0
    current_time = 0
    count = 0
    while current_time < title_length:
      # We want to test playing only for ten seconds
      count = count + 1
      if count == 10:
        break
      current_time = self._CurrentPlaybackTime()
      self.assertTrue(prev_time <= current_time,
          msg='Prev playing time %s is greater than current time %s'
          % (prev_time, current_time))
      prev_time = current_time
      # play video for some time
      time.sleep(1)
    # crosbug.com/22037
    # In case player doesn't start playing at all, above while loop may
    # still pass. So re-verifying and assuming that player did play something
    # during last 10 seconds.
    self.assertTrue(current_time > 0,
        msg='Netflix player didnot start playing')

if __name__ == '__main__':
  pyauto_functional.Main()
<|endoftext|>


<commit_before># This file is part of Fedora Community.
# Copyright (C) 2008-2010  Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from moksha.connector import IConnector, ICall, IQuery, ParamFilter
from pylons import config
from urllib import quote
from fedoracommunity.search import utils, distmappings
from fedoracommunity.lib.utils import OrderedDict
import os
import sys
import xapian
import cgi

try:
    import json
except ImportError:
    import simplejson as json

class XapianConnector(IConnector, ICall, IQuery):
    _method_paths = {}
    _query_paths = {}

    def __init__(self, environ=None, request=None):
        super(XapianConnector, self).__init__(environ, request)
        self._search_db = xapian.Database(config.get('fedoracommunity.connector.xapian.package-search.db', 'xapian/search'))
        self._versionmap_db = xapian.Database(config.get('fedoracommunity.connector.xapian.versionmap.db', 'xapian/versionmap'))

    # IConnector
    @classmethod
    def register(cls):
        cls.register_search_packages()

    def introspect(self):
        # FIXME: return introspection data
        return None

    @classmethod
    def register_search_packages(cls):
        path = cls.register_query(
                      'search_packages',
                      cls.search_packages,
                      primary_key_col = 'name',
                      default_sort_col = 'name',
                      default_sort_order = -1,
                      can_paginate = True)

        path.register_column('name',
                        default_visible = True,
                        can_sort = False,
                        can_filter_wildcards = False)

        path.register_column('summary',
                        default_visible = True,
                        can_sort = False,
                        can_filter_wildcards = False)

    def _highlight_str(self, string, term):
        # we are injecting html so url escape the origional string
        # to avoid an html injection attack from the packages themselves
        # string = cgi.escape(string)
        lc_string = string.lower()
        term = term.lower()
        term_len = len(term)
        result = ''

        i = lc_string.find(term, 0)
        start = 0
        while i != -1:
            result += string[start:i] + ''
            start = i + term_len
            result += string[i:start] + ''
            i = lc_string.find(term, start)

        result += string[start:]
        return result

    def _highlight_matches(self, row_data, term):
        # make link from name before we potentially rewrite it
        # if we haven't already
        if 'link' not in row_data:
            row_data['link'] = row_data['name']
        row_data['name'] = self._highlight_str(row_data['name'], term);
        row_data['summary'] = self._highlight_str(row_data['summary'], term);
        row_data['description'] = self._highlight_str(row_data['description'], term);
        for pkg in row_data['sub_pkgs']:
            if 'link' not in pkg:
                pkg['link'] = pkg['name']
            pkg['name'] = self._highlight_str(pkg['name'], term);
            pkg['summary'] = self._highlight_str(pkg['summary'], term);
            pkg['description'] = self._highlight_str(pkg['description'], term);

    def search_packages(self, start_row=None,
                              rows_per_page=None,
                              order=-1,
                              sort_col=None,
                              filters = {},
                              **params):

        search_string = filters.get('search')

        unfiltered_search_terms = search_string.split(' ')

        search_string = utils.filter_search_string (search_string)
        phrase = '"%s"' % search_string

        # add exact matchs
        search_terms = search_string.split(' ')
        for term in search_terms:
            if term == '':
                continue

            search_string += " EX__%s__EX" % term

        # add phrase match
        search_string += phrase

        matches = self.do_search(search_string,
                                 start_row,
                                 rows_per_page,
                                 order,
                                 sort_col)

        count = matches.get_matches_estimated()
        rows = []
        for m in matches:
            result = json.loads(m.document.get_data())

            # mark matches in
            for term in unfiltered_search_terms:
                if term == '':
                    continue

                self._highlight_matches(result, term)

            rows.append(result)

        return (count, rows)

    def get_package_info(self, package_name):
        package_name = utils.filter_search_string(package_name)
        search_string = "Ex__%s__EX" % package_name
        matches = self.do_search(search_string, 0, 1)
        if len(matches) == 0:
            return None

        result = json.loads(matches[0].document.get_data())
        return result

    def do_search(self,
                  search_string,
                  start_row=None,
                  rows_per_page=None,
                  order=-1,
                  sort_col=None):
        enquire = xapian.Enquire(self._search_db)
        qp = xapian.QueryParser()
        qp.set_database(self._search_db)
        query = qp.parse_query(search_string)

        enquire.set_query(query)
        matches = enquire.get_mset(start_row, rows_per_page)

        return matches

    def get_latest_builds(self, package_name):
        enquire = xapian.Enquire(self._versionmap_db)
        qp = xapian.QueryParser()
        qp.set_database(self._versionmap_db)
        qp.add_boolean_prefix('key', 'XA')
        query = qp.parse_query('key:%s' % utils.filter_search_string(package_name))

        enquire.set_query(query)
        matches = enquire.get_mset(0, 1)
        if len(matches) == 0:
            return None
        results = json.loads(matches[0].document.get_data())

        latest_builds = OrderedDict()
        lastdistname = ""

        for dist in distmappings.tags:
            distname = dist['name']
            if lastdistname != distname and distname in results:
                latest_builds[distname] = results[distname]
                lastdistname = distname

        return latest_builds
<commit_msg>use near phrase searching also<commit_after># This file is part of Fedora Community.
# Copyright (C) 2008-2010  Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from moksha.connector import IConnector, ICall, IQuery, ParamFilter
from pylons import config
from urllib import quote
from fedoracommunity.search import utils, distmappings
from fedoracommunity.lib.utils import OrderedDict
import os
import sys
import xapian
import cgi

try:
    import json
except ImportError:
    import simplejson as json

class XapianConnector(IConnector, ICall, IQuery):
    _method_paths = {}
    _query_paths = {}

    def __init__(self, environ=None, request=None):
        super(XapianConnector, self).__init__(environ, request)
        self._search_db = xapian.Database(config.get('fedoracommunity.connector.xapian.package-search.db', 'xapian/search'))
        self._versionmap_db = xapian.Database(config.get('fedoracommunity.connector.xapian.versionmap.db', 'xapian/versionmap'))

    # IConnector
    @classmethod
    def register(cls):
        cls.register_search_packages()

    def introspect(self):
        # FIXME: return introspection data
        return None

    @classmethod
    def register_search_packages(cls):
        path = cls.register_query(
                      'search_packages',
                      cls.search_packages,
                      primary_key_col = 'name',
                      default_sort_col = 'name',
                      default_sort_order = -1,
                      can_paginate = True)

        path.register_column('name',
                        default_visible = True,
                        can_sort = False,
                        can_filter_wildcards = False)

        path.register_column('summary',
                        default_visible = True,
                        can_sort = False,
                        can_filter_wildcards = False)

    def _highlight_str(self, string, term):
        # we are injecting html so url escape the origional string
        # to avoid an html injection attack from the packages themselves
        # string = cgi.escape(string)
        lc_string = string.lower()
        term = term.lower()
        term_len = len(term)
        result = ''

        i = lc_string.find(term, 0)
        start = 0
        while i != -1:
            result += string[start:i] + ''
            start = i + term_len
            result += string[i:start] + ''
            i = lc_string.find(term, start)

        result += string[start:]
        return result

    def _highlight_matches(self, row_data, term):
        # make link from name before we potentially rewrite it
        # if we haven't already
        if 'link' not in row_data:
            row_data['link'] = row_data['name']
        row_data['name'] = self._highlight_str(row_data['name'], term);
        row_data['summary'] = self._highlight_str(row_data['summary'], term);
        row_data['description'] = self._highlight_str(row_data['description'], term);
        for pkg in row_data['sub_pkgs']:
            if 'link' not in pkg:
                pkg['link'] = pkg['name']
            pkg['name'] = self._highlight_str(pkg['name'], term);
            pkg['summary'] = self._highlight_str(pkg['summary'], term);
            pkg['description'] = self._highlight_str(pkg['description'], term);

    def search_packages(self, start_row=None,
                              rows_per_page=None,
                              order=-1,
                              sort_col=None,
                              filters = {},
                              **params):

        search_string = filters.get('search')

        unfiltered_search_terms = search_string.split(' ')

        search_string = utils.filter_search_string (search_string)
        phrase = '"%s"' % search_string

        # add exact matchs
        search_terms = search_string.split(' ')
        for term in search_terms:
            if term == '':
                continue

            search_string += " EX__%s__EX" % term

        # add phrase match
        search_string += " OR %s" % phrase
        # add near phrase match
        search_string += " OR (%s)" % ' NEAR '.join(search_terms)

        matches = self.do_search(search_string,
                                 start_row,
                                 rows_per_page,
                                 order,
                                 sort_col)

        count = matches.get_matches_estimated()
        rows = []
        for m in matches:
            result = json.loads(m.document.get_data())

            # mark matches in
            for term in unfiltered_search_terms:
                if term == '':
                    continue

                self._highlight_matches(result, term)

            rows.append(result)

        return (count, rows)

    def get_package_info(self, package_name):
        package_name = utils.filter_search_string(package_name)
        search_string = "Ex__%s__EX" % package_name
        matches = self.do_search(search_string, 0, 1)
        if len(matches) == 0:
            return None

        result = json.loads(matches[0].document.get_data())
        return result

    def do_search(self,
                  search_string,
                  start_row=None,
                  rows_per_page=None,
                  order=-1,
                  sort_col=None):
        enquire = xapian.Enquire(self._search_db)
        qp = xapian.QueryParser()
        qp.set_database(self._search_db)
        query = qp.parse_query(search_string)

        enquire.set_query(query)
        matches = enquire.get_mset(start_row, rows_per_page)

        return matches

    def get_latest_builds(self, package_name):
        enquire = xapian.Enquire(self._versionmap_db)
        qp = xapian.QueryParser()
        qp.set_database(self._versionmap_db)
        qp.add_boolean_prefix('key', 'XA')
        query = qp.parse_query('key:%s' % utils.filter_search_string(package_name))

        enquire.set_query(query)
        matches = enquire.get_mset(0, 1)
        if len(matches) == 0:
            return None
        results = json.loads(matches[0].document.get_data())

        latest_builds = OrderedDict()
        lastdistname = ""

        for dist in distmappings.tags:
            distname = dist['name']
            if lastdistname != distname and distname in results:
                latest_builds[distname] = results[distname]
                lastdistname = distname

        return latest_builds
<|endoftext|>


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

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

"""
Hotel California

In this example we show how to keep people in the same room. Following command
is used:

- command: open
- response: **Hotel California** mode activated!

- command: close
- response: **Hotel California** mode deactivated!

- command: hotel
- provides current status of the hotel: open or close

To run this script you have to provide a custom configuration, or set
environment variables instead::

- ``CHAT_ROOM_MODERATORS`` - Mention at least your e-mail address
- ``CISCO_SPARK_BOT_TOKEN`` - Received from Cisco Spark on bot registration
- ``CISCO_SPARK_TOKEN`` - Your personal Cisco Spark token
- ``SERVER_URL`` - Public link used by Cisco Spark to reach your server

The token is specific to your run-time, please visit Cisco Spark for
Developers to get more details:

    https://developer.ciscospark.com/

The other token should be associated to a human being, and not to a bot.
This is required so that the software can receive all events for a chat space.
Without it, the bot may not see who is leaving or joining.

For example, if you run this script under Linux or macOs with support from
ngrok for exposing services to the Internet::

    export CHAT_ROOM_MODERATORS="alice@acme.com"
    export CISCO_SPARK_BOT_TOKEN="<token id from Cisco Spark for Developers>"
    export CISCO_SPARK_TOKEN="<personal token id from Cisco Spark>"
    export SERVER_URL="http://1a107f21.ngrok.io"
    python hotel_california.py

Credit: https://github.com/flint-bot/flint
"""

import os
import time

from shellbot import Engine, ShellBot, Context, Command
Context.set_logger()

#
# create a bot and load commands
#

class Open(Command):
    keyword = 'open'
    information_message = u"Open Hotel California"

    def execute(self, bot, arguments=None):
        if bot.recall('hotel_california.state', 'off') == 'on':
            bot.say('Hotel California mode is already activated!')
        else:
            bot.remember('hotel_california.state', 'on')
            bot.say('Hotel California mode activated!')

class Close(Command):
    keyword = 'close'
    information_message = u"Close Hotel California"

    def execute(self, bot, arguments=None):
        if bot.recall('hotel_california.state', 'off') == 'off':
            bot.say('Hotel California mode is already deactivated!')
        else:
            bot.remember('hotel_california.state', 'off')
            bot.say('Hotel California mode deactivated!')

class Hotel(Command):
    keyword = 'hotel'
    information_message = u"Get status of Hotel California"

    def execute(self, bot, arguments=None):
        if bot.recall('hotel_california.state', 'off') == 'off':
            bot.say('Hotel California will let you escape')
        else:
            bot.say('Hotel California will keep you here forever!')

engine = Engine(type='spark', commands=[Open(), Close(), Hotel()])

# load configuration
#
os.environ['BOT_ON_START'] = 'On a dark desert highway, cool wind in my hair...'
os.environ['CHAT_ROOM_TITLE'] = 'Hotel California'
engine.configure()

# initialise a chat room
#
bot = engine.bond(reset=True)

# add stickiness to the hotel
#

class Magic(object):

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

    def on_join(self, received):

        bot = self.engine.get_bot(received.channel_id)
        addresses = bot.recall('visitors', [])

        if received.actor_address not in addresses:
            addresses.append(received.actor_address)
            bot.remember('visitors', addresses)
            bot.say(u"Welcome to Hotel California, {}".format(received.actor_label))

    def on_leave(self, received):

        bot = self.engine.get_bot(received.channel_id)
        addresses = bot.recall('visitors', [])

        if bot.recall('hotel_california.state', 'off') == 'off':
            try:
                addresses.remove(received.actor_address)
            except ValueError:
                pass
            bot.remember('visitors', addresses)
            bot.say('On a dark desert highway, cool wind in my hair...')

        else:
            bot.say('Such a lovely place...')
            time.sleep(5)
            bot.add_participant(received.actor_address)
            bot.say(
                u'{}, you can check out any time you like, but you can never leave!'.format(received.actor_label),
                content=u'<@personEmail:{}|{}>, you can **check out any time you like**, but you can **never** leave!'.format(received.actor_address, received.actor_label))

magic = Magic(engine=engine)
engine.subscribe('join', magic)
engine.subscribe('leave', magic)

# run the bot
#
engine.run()

# delete the chat room when the bot is stopped
#
bot.dispose()
<commit_msg>adapt examples/hotel_california -- now available in any room too<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

"""
Hotel California

In this example we show how to keep people in the same room. Following command
is used:

- command: open
- response: **Hotel California** mode activated!

- command: close
- response: **Hotel California** mode deactivated!

- command: hotel
- provides current status of the hotel: open or close

To run this script you have to provide a custom configuration, or set
environment variables instead::

- ``CHAT_ROOM_MODERATORS`` - Mention at least your e-mail address
- ``CISCO_SPARK_BOT_TOKEN`` - Received from Cisco Spark on bot registration
- ``CISCO_SPARK_TOKEN`` - Your personal Cisco Spark token
- ``SERVER_URL`` - Public link used by Cisco Spark to reach your server

The token is specific to your run-time, please visit Cisco Spark for
Developers to get more details:

    https://developer.ciscospark.com/

The other token should be associated to a human being, and not to a bot.
This is required so that the software can receive all events for a chat space.
Without it, the bot may not see who is leaving or joining.

For example, if you run this script under Linux or macOs with support from
ngrok for exposing services to the Internet::

    export CHAT_ROOM_MODERATORS="alice@acme.com"
    export CISCO_SPARK_BOT_TOKEN="<token id from Cisco Spark for Developers>"
    export CISCO_SPARK_TOKEN="<personal token id from Cisco Spark>"
    export SERVER_URL="http://1a107f21.ngrok.io"
    python hotel_california.py

Credit: https://github.com/flint-bot/flint
"""

import os
import time

from shellbot import Engine, ShellBot, Context, Command
Context.set_logger()

#
# create a bot and load commands
#

class Open(Command):
    keyword = 'open'
    information_message = u"Open Hotel California"

    def execute(self, bot, arguments=None):
        if bot.recall('hotel_california.state', 'off') == 'on':
            bot.say('Hotel California mode is already activated!')
        else:
            bot.remember('hotel_california.state', 'on')
            bot.say('Hotel California mode activated!')

class Close(Command):
    keyword = 'close'
    information_message = u"Close Hotel California"

    def execute(self, bot, arguments=None):
        if bot.recall('hotel_california.state', 'off') == 'off':
            bot.say('Hotel California mode is already deactivated!')
        else:
            bot.remember('hotel_california.state', 'off')
            bot.say('Hotel California mode deactivated!')

class Hotel(Command):
    keyword = 'hotel'
    information_message = u"Get status of Hotel California"

    def execute(self, bot, arguments=None):
        if bot.recall('hotel_california.state', 'off') == 'off':
            bot.say('Hotel California will let you escape')
        else:
            bot.say('Hotel California will keep you here forever!')

engine = Engine(type='spark', commands=[Open(), Close(), Hotel()])

# load configuration
#
os.environ['BOT_ON_ENTER'] = 'On a dark desert highway, cool wind in my hair...'
os.environ['CHAT_ROOM_TITLE'] = 'Hotel California'
engine.configure()

# initialise a chat room
#
bot = engine.get_bot(reset=True)

# add stickiness to the hotel
#

class Magic(object):

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

    def on_join(self, received):

        bot = self.engine.get_bot(received.channel_id)
        addresses = bot.recall('visitors', [])

        if received.actor_address not in addresses:
            addresses.append(received.actor_address)
            bot.remember('visitors', addresses)
            bot.say(u"Welcome to Hotel California, {}".format(received.actor_label))

    def on_leave(self, received):

        bot = self.engine.get_bot(received.channel_id)
        addresses = bot.recall('visitors', [])

        if bot.recall('hotel_california.state', 'off') == 'off':
            try:
                addresses.remove(received.actor_address)
            except ValueError:
                pass
            bot.remember('visitors', addresses)
            bot.say('On a dark desert highway, cool wind in my hair...')

        else:
            bot.say('Such a lovely place...')
            time.sleep(5)
            bot.add_participant(received.actor_address)
            bot.say(
                u'{}, you can check out any time you like, but you can never leave!'.format(received.actor_label),
                content=u'<@personEmail:{}|{}>, you can **check out any time you like**, but you can **never** leave!'.format(received.actor_address, received.actor_label))

magic = Magic(engine=engine)
engine.subscribe('join', magic)
engine.subscribe('leave', magic)

# run the bot
#
engine.run()

# delete the chat room when the bot is stopped
#
bot.dispose()
<|endoftext|>


<commit_before># Copyright (c) 2020, Djaodjin Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
PEP 386-compliant version number for the multitier django app.
"""

__version__ = '0.1.15'
<commit_msg>bumps version number to 0.1.16-dev<commit_after># Copyright (c) 2020, Djaodjin Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
PEP 386-compliant version number for the multitier django app.
"""

__version__ = '0.1.16-dev'
<|endoftext|>


<commit_before>from tqdm import tqdm

import numpy as np

class Core(object):
    """
    Implements the functions to run a generic algorithm.

    """
    def __init__(self, agent, mdp, callbacks=None):
        """
        Constructor.

        Args:
            agent (Agent): the agent moving according to a policy;
            mdp (Environment): the environment in which the agent moves;
            callbacks (list): list of callbacks to execute at the end of
                each learn iteration.

        """
        self.agent = agent
        self.mdp = mdp
        self.callbacks = callbacks if callbacks is not None else list()

        self._state = None

        self._total_episodes_counter = 0
        self._total_steps_counter = 0
        self._current_episodes_counter = 0
        self._current_steps_counter = 0
        self._episode_steps = None
        self._n_steps = None
        self._n_episodes = None
        self._n_steps_per_fit = None
        self._n_episodes_per_fit = None

    def learn(self, n_steps=None, n_episodes=None, n_steps_per_fit=None,
              n_episodes_per_fit=None, render=False, quiet=False):
        """
        This function moves the agent in the environment and fits the policy
        using the collected samples. The agent can be moved for a given number
        of steps or a given number of episodes and, independently from this
        choice, the policy can be fitted after a given number of steps or a
        given number of episodes.

        Args:
            n_steps (int, None): number of steps to move the agent;
            n_episodes (int, None): number of episodes to move the agent;
            n_steps_per_fit (int, None): number of steps between each fit of the
                policy;
            n_episodes_per_fit (int, None): number of episodes between each fit
                of the policy;
            render (bool, False): whether to render the environment or not;
            quiet (bool, False): whether to show the progress bar or not.

        """
        assert (n_episodes_per_fit is not None and n_steps_per_fit is None)\
            or (n_episodes_per_fit is None and n_steps_per_fit is not None)

        self._n_steps_per_fit = n_steps_per_fit
        self._n_episodes_per_fit = n_episodes_per_fit

        if n_steps_per_fit is not None:
            fit_condition =\
                lambda: self._current_steps_counter >= self._n_steps_per_fit
        else:
            fit_condition = lambda: self._current_episodes_counter\
                                     >= self._n_episodes_per_fit

        self._run(n_steps, n_episodes, fit_condition, render, quiet)

    def evaluate(self, initial_states=None, n_steps=None, n_episodes=None,
                 render=False, quiet=False):
        """
        This function moves the agent in the environment using its policy.
        The agent is moved for a provided number of steps, episodes, or from
        a set of initial states for the whole episode.

        Args:
            initial_states (np.ndarray, None): the starting states of each
                episode;
            n_steps (int, None): number of steps to move the agent;
            n_episodes (int, None): number of episodes to move the agent;
            render (bool, False): whether to render the environment or not;
            quiet (bool, False): whether to show the progress bar or not.

        """
        fit_condition = lambda: False

        return self._run(n_steps, n_episodes, fit_condition, render, quiet,
                         initial_states)

    def _run(self, n_steps, n_episodes, fit_condition, render, quiet,
             initial_states=None):
        assert n_episodes is not None and n_steps is None and initial_states is None\
            or n_episodes is None and n_steps is not None and initial_states is None\
            or n_episodes is None and n_steps is None and initial_states is not None

        self._n_steps = n_steps
        self._n_episodes = len(
            initial_states) if initial_states is not None else n_episodes

        if n_steps is not None:
            move_condition =\
                lambda: self._total_steps_counter < self._n_steps

            steps_progress_bar = tqdm(total=self._n_steps,
                                      dynamic_ncols=True, disable=quiet,
                                      leave=False)
            episodes_progress_bar = tqdm(disable=True)
        else:
            move_condition =\
                lambda: self._total_episodes_counter < self._n_episodes

            steps_progress_bar = tqdm(disable=True)
            episodes_progress_bar = tqdm(total=self._n_episodes,
                                         dynamic_ncols=True, disable=quiet,
                                         leave=False)

        return self._run_impl(move_condition, fit_condition, steps_progress_bar,
                              episodes_progress_bar, render, initial_states)

    def _run_impl(self, move_condition, fit_condition, steps_progress_bar,
                  episodes_progress_bar, render, initial_states):
        self._total_episodes_counter = 0
        self._total_steps_counter = 0
        self._current_episodes_counter = 0
        self._current_steps_counter = 0

        dataset = list()
        last = True
        while move_condition():

            if last:
                self.reset(initial_states)

            sample = self._step(render)
            dataset.append(sample)
            self._total_steps_counter += 1
            self._current_steps_counter += 1
            steps_progress_bar.update(1)

            if sample[-1]:
                self._total_episodes_counter += 1
                self._current_episodes_counter += 1
                episodes_progress_bar.update(1)

            if fit_condition():
                self.agent.fit(dataset)
                self._current_episodes_counter = 0
                self._current_steps_counter = 0

                for c in self.callbacks:
                    callback_pars = dict(dataset=dataset)
                    c(**callback_pars)

                dataset = list()

            last = sample[-1]

        self.agent.stop()
        self.mdp.stop()

        return dataset

    def _step(self, render):
        """
        Single step.

        Args:
            render (bool): whether to render or not.

        Returns:
            A tuple containing the previous state, the action sampled by the
            agent, the reward obtained, the reached state, the absorbing flag
            of the reached state and the last step flag.

        """
        action = self.agent.draw_action(self._state)
        next_state, reward, absorbing, _ = self.mdp.step(action)

        self._episode_steps += 1

        if render:
            self.mdp.render()

        last = not(
            self._episode_steps < self.mdp.info.horizon and not absorbing)

        state = self._state
        self._state = np.array(next_state)  # Copy for safety reasons

        return state, action, reward, next_state, absorbing, last

    def reset(self, initial_states=None):
        """
        Reset the state of the agent.

        """
        if initial_states is None\
            or self._total_episodes_counter == self._n_episodes:
            initial_state = None
        else:
            initial_state = initial_states[self._total_episodes_counter]

        self._state = self.mdp.reset(initial_state)
        self.agent.episode_start()
        self._episode_steps = 0
<commit_msg>resume flag added in core<commit_after>from tqdm import tqdm

import numpy as np

class Core(object):
    """
    Implements the functions to run a generic algorithm.

    """
    def __init__(self, agent, mdp, callbacks=None):
        """
        Constructor.

        Args:
            agent (Agent): the agent moving according to a policy;
            mdp (Environment): the environment in which the agent moves;
            callbacks (list): list of callbacks to execute at the end of
                each learn iteration.

        """
        self.agent = agent
        self.mdp = mdp
        self.callbacks = callbacks if callbacks is not None else list()

        self._state = None

        self._total_episodes_counter = 0
        self._total_steps_counter = 0
        self._current_episodes_counter = 0
        self._current_steps_counter = 0
        self._episode_steps = None
        self._n_steps = None
        self._n_episodes = None
        self._n_steps_per_fit = None
        self._n_episodes_per_fit = None

    def learn(self, n_steps=None, n_episodes=None, n_steps_per_fit=None,
              n_episodes_per_fit=None, render=False, quiet=False, resume=False):
        """
        This function moves the agent in the environment and fits the policy
        using the collected samples. The agent can be moved for a given number
        of steps or a given number of episodes and, independently from this
        choice, the policy can be fitted after a given number of steps or a
        given number of episodes. By default, the environment is reset.

        Args:
            n_steps (int, None): number of steps to move the agent;
            n_episodes (int, None): number of episodes to move the agent;
            n_steps_per_fit (int, None): number of steps between each fit of the
                policy;
            n_episodes_per_fit (int, None): number of episodes between each fit
                of the policy;
            render (bool, False): whether to render the environment or not;
            quiet (bool, False): whether to show the progress bar or not;
            resume (bool, False): whether to restart from the last state reached
                or reset the mdp.

        """
        assert (n_episodes_per_fit is not None and n_steps_per_fit is None)\
            or (n_episodes_per_fit is None and n_steps_per_fit is not None)

        self._n_steps_per_fit = n_steps_per_fit
        self._n_episodes_per_fit = n_episodes_per_fit

        if n_steps_per_fit is not None:
            fit_condition =\
                lambda: self._current_steps_counter >= self._n_steps_per_fit
        else:
            fit_condition = lambda: self._current_episodes_counter\
                                     >= self._n_episodes_per_fit

        self._run(n_steps, n_episodes, fit_condition, render, quiet, resume)

    def evaluate(self, initial_states=None, n_steps=None, n_episodes=None,
                 render=False, quiet=False, resume=False):
        """
        This function moves the agent in the environment using its policy.
        The agent is moved for a provided number of steps, episodes, or from
        a set of initial states for the whole episode. By default, the
        environment is reset.

        Args:
            initial_states (np.ndarray, None): the starting states of each
                episode;
            n_steps (int, None): number of steps to move the agent;
            n_episodes (int, None): number of episodes to move the agent;
            render (bool, False): whether to render the environment or not;
            quiet (bool, False): whether to show the progress bar or not;
            resume (bool, False): whether to restart from the last state reached
                or reset the mdp.

        """
        fit_condition = lambda: False

        return self._run(n_steps, n_episodes, fit_condition, render, quiet,
                         resume, initial_states)

    def _run(self, n_steps, n_episodes, fit_condition, render, quiet,
             resume, initial_states=None):
        assert n_episodes is not None and n_steps is None and initial_states is None\
            or n_episodes is None and n_steps is not None and initial_states is None\
            or n_episodes is None and n_steps is None and initial_states is not None

        self._n_steps = n_steps
        self._n_episodes = len(
            initial_states) if initial_states is not None else n_episodes

        if n_steps is not None:
            move_condition =\
                lambda: self._total_steps_counter < self._n_steps

            steps_progress_bar = tqdm(total=self._n_steps,
                                      dynamic_ncols=True, disable=quiet,
                                      leave=False)
            episodes_progress_bar = tqdm(disable=True)
        else:
            move_condition =\
                lambda: self._total_episodes_counter < self._n_episodes

            steps_progress_bar = tqdm(disable=True)
            episodes_progress_bar = tqdm(total=self._n_episodes,
                                         dynamic_ncols=True, disable=quiet,
                                         leave=False)

        return self._run_impl(move_condition, fit_condition, steps_progress_bar,
                              episodes_progress_bar, render, initial_states,
                              resume)

    def _run_impl(self, move_condition, fit_condition, steps_progress_bar,
                  episodes_progress_bar, render, initial_states, resume):
        self._total_episodes_counter = 0
        self._total_steps_counter = 0
        self._current_episodes_counter = 0
        self._current_steps_counter = 0

        dataset = list()

        if not resume:
            self.reset(initial_states)

        while move_condition():
            sample = self._step(render)
            dataset.append(sample)
            self._total_steps_counter += 1
            self._current_steps_counter += 1
            steps_progress_bar.update(1)

            if sample[-1]:
                self._total_episodes_counter += 1
                self._current_episodes_counter += 1
                episodes_progress_bar.update(1)

            if fit_condition():
                self.agent.fit(dataset)
                self._current_episodes_counter = 0
                self._current_steps_counter = 0

                for c in self.callbacks:
                    callback_pars = dict(dataset=dataset)
                    c(**callback_pars)

                dataset = list()

            if sample[-1]:
                self.reset(initial_states)

        self.agent.stop()
        self.mdp.stop()

        return dataset

    def _step(self, render):
        """
        Single step.

        Args:
            render (bool): whether to render or not.

        Returns:
            A tuple containing the previous state, the action sampled by the
            agent, the reward obtained, the reached state, the absorbing flag
            of the reached state and the last step flag.

        """
        action = self.agent.draw_action(self._state)
        next_state, reward, absorbing, _ = self.mdp.step(action)

        self._episode_steps += 1

        if render:
            self.mdp.render()

        last = not(
            self._episode_steps < self.mdp.info.horizon and not absorbing)

        state = self._state
        self._state = np.array(next_state)  # Copy for safety reasons

        return state, action, reward, next_state, absorbing, last

    def reset(self, initial_states=None):
        """
        Reset the state of the agent.

        """
        if initial_states is None\
            or self._total_episodes_counter == self._n_episodes:
            initial_state = None
        else:
            initial_state = initial_states[self._total_episodes_counter]

        self._state = self.mdp.reset(initial_state)
        self.agent.episode_start()
        self._episode_steps = 0
<|endoftext|>


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

import os
import argparse
import multiprocessing as mp
import neblio_ci_libs as nci

nci.setup_travis_or_gh_actions_env_vars()

working_dir = os.getcwd()
build_dir = "build"
deploy_dir = os.path.join(os.environ['BUILD_DIR'],'deploy', '')

parser = argparse.ArgumentParser()
parser.add_argument('--test', '-t', help='Only build and run tests', action='store_true')
args = parser.parse_args()

nci.mkdir_p(deploy_dir)
nci.mkdir_p(build_dir)
os.chdir(build_dir)

# do not auto update homebrew as it is very slow
os.environ['HOMEBREW_NO_AUTO_UPDATE'] = '1'

# set up ccache
nci.call_with_err_code('brew fetch --retry ccache        && brew install ccache --force')

nci.call_with_err_code('brew fetch --retry qt            && brew install qt --force')
nci.call_with_err_code('brew fetch --retry berkeley-db@4 && brew install berkeley-db@4 --force')
nci.call_with_err_code('brew fetch --retry boost@1.60    && brew install boost@1.60 --force')
nci.call_with_err_code('brew fetch --retry miniupnpc     && brew install miniupnpc --force')
nci.call_with_err_code('brew fetch --retry curl          && brew install curl --force')
nci.call_with_err_code('brew fetch --retry openssl       && brew install openssl --force')
nci.call_with_err_code('brew fetch --retry qrencode      && brew install qrencode --force')

nci.call_with_err_code('brew unlink qt            && brew link --force --overwrite qt')
nci.call_with_err_code('brew unlink berkeley-db@4 && brew link --force --overwrite berkeley-db@4')
nci.call_with_err_code('brew unlink boost@1.60    && brew link --force --overwrite boost@1.60')
nci.call_with_err_code('brew unlink miniupnpc     && brew link --force --overwrite miniupnpc')
nci.call_with_err_code('brew unlink curl          && brew link --force --overwrite curl')
nci.call_with_err_code('brew unlink python        && brew link --force --overwrite python')
nci.call_with_err_code('brew unlink openssl       && brew link --force --overwrite openssl')
nci.call_with_err_code('brew unlink qrencode      && brew link --force --overwrite qrencode')

nci.call_with_err_code('ccache -s')

# prepend ccache to the path, necessary since prior steps prepend things to the path
os.environ['PATH'] = '/usr/local/opt/ccache/libexec:' + os.environ['PATH']

if (args.test):
    nci.call_with_err_code('qmake "QMAKE_CXX=ccache clang++" "USE_UPNP=1" "USE_QRCODE=1" "RELEASE=1" "NEBLIO_CONFIG += NoWallet" ../neblio-wallet.pro')
    nci.call_with_err_code("make -j" + str(mp.cpu_count()))
    # run tests
    nci.call_with_err_code("./wallet/test/neblio-Qt.app/Contents/MacOS/neblio-Qt")
else:
    nci.call_with_err_code('qmake "QMAKE_CXX=ccache clang++" "USE_UPNP=1" "USE_QRCODE=1" "RELEASE=1" ../neblio-wallet.pro')
    nci.call_with_err_code("make -j" + str(mp.cpu_count()))
    # build our .dmg
    nci.call_with_err_code('npm install -g appdmg')
    os.chdir("wallet")
    nci.call_with_err_code('../../contrib/macdeploy/macdeployqtplus ./neblio-Qt.app -add-qt-tr da,de,es,hu,ru,uk,zh_CN,zh_TW -verbose 1 -rpath /usr/local/opt/qt/lib')
    nci.call_with_err_code('appdmg ../../contrib/macdeploy/appdmg.json ./neblio-Qt.dmg')

    file_name = '$(date +%Y-%m-%d)---' + os.environ['BRANCH'] + '-' + os.environ['COMMIT'][:7] + '---neblio-Qt---macOS.zip'

    nci.call_with_err_code('zip -j ' + file_name + ' ./neblio-Qt.dmg')
    nci.call_with_err_code('mv ' + file_name + ' ' + deploy_dir)
    nci.call_with_err_code('echo "Binary package at ' + deploy_dir + file_name + '"')
    # set the SOURCE_DIR env var, this points to the binary that will be uploaded
    nci.call_with_err_code('echo "::set-env name=SOURCE_PATH::' + deploy_dir + file_name + '"')

nci.call_with_err_code('ccache -s')

print("")
print("")
print("Building finished successfully.")
print("")
<commit_msg>Update test_osx-gui_wallet.py<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import argparse
import multiprocessing as mp
import neblio_ci_libs as nci

nci.setup_travis_or_gh_actions_env_vars()

working_dir = os.getcwd()
build_dir = "build"
deploy_dir = os.path.join(os.environ['BUILD_DIR'],'deploy', '')

parser = argparse.ArgumentParser()
parser.add_argument('--test', '-t', help='Only build and run tests', action='store_true')
args = parser.parse_args()

nci.mkdir_p(deploy_dir)
nci.mkdir_p(build_dir)
os.chdir(build_dir)

# do not auto update homebrew as it is very slow
os.environ['HOMEBREW_NO_AUTO_UPDATE'] = '1'

# set up ccache
nci.call_with_err_code('brew fetch --retry ccache        && brew install ccache --force')

nci.call_with_err_code('brew fetch --retry qt            && brew install qt --force')
nci.call_with_err_code('brew fetch --retry berkeley-db@4 && brew install berkeley-db@4 --force')
nci.call_with_err_code('brew fetch --retry boost@1.60    && brew install boost@1.60 --force')
nci.call_with_err_code('brew fetch --retry miniupnpc     && brew install miniupnpc --force')
nci.call_with_err_code('brew fetch --retry curl          && brew install curl --force')
nci.call_with_err_code('brew fetch --retry openssl       && brew install openssl --force')
nci.call_with_err_code('brew fetch --retry qrencode      && brew install qrencode --force')

nci.call_with_err_code('brew unlink qt            && brew link --force --overwrite qt')
nci.call_with_err_code('brew unlink berkeley-db@4 && brew link --force --overwrite berkeley-db@4')
nci.call_with_err_code('brew unlink boost@1.60    && brew link --force --overwrite boost@1.60')
nci.call_with_err_code('brew unlink miniupnpc     && brew link --force --overwrite miniupnpc')
nci.call_with_err_code('brew unlink curl          && brew link --force --overwrite curl')
nci.call_with_err_code('brew unlink python        && brew link --force --overwrite python')
nci.call_with_err_code('brew unlink openssl       && brew link --force --overwrite openssl')
nci.call_with_err_code('brew unlink qrencode      && brew link --force --overwrite qrencode')

nci.call_with_err_code('ccache -s')

# prepend ccache to the path, necessary since prior steps prepend things to the path
os.environ['PATH'] = '/usr/local/opt/ccache/libexec:' + os.environ['PATH']

if (args.test):
    nci.call_with_err_code('qmake "QMAKE_CXX=ccache clang++" "USE_UPNP=1" "USE_QRCODE=1" "RELEASE=1" "NEBLIO_CONFIG += NoWallet" ../neblio-wallet.pro')
    nci.call_with_err_code("make -j" + str(mp.cpu_count()))
    # run tests
    nci.call_with_err_code("./wallet/test/neblio-Qt.app/Contents/MacOS/neblio-Qt")
else:
    nci.call_with_err_code('qmake "QMAKE_CXX=ccache clang++" "USE_UPNP=1" "USE_QRCODE=1" "RELEASE=1" ../neblio-wallet.pro')
    nci.call_with_err_code("make -j" + str(mp.cpu_count()))
    # build our .dmg
    nci.call_with_err_code('npm install -g appdmg')
    os.chdir("wallet")
    nci.call_with_err_code('../../contrib/macdeploy/macdeployqtplus ./neblio-Qt.app -add-qt-tr da,de,es,hu,ru,uk,zh_CN,zh_TW -verbose 1 -rpath /usr/local/opt/qt/lib')
    nci.call_with_err_code('appdmg ../../contrib/macdeploy/appdmg.json ./neblio-Qt.dmg')
    os.chdir("ls -al")
    os.chdir("ls -al dist")

    file_name = '$(date +%Y-%m-%d)---' + os.environ['BRANCH'] + '-' + os.environ['COMMIT'][:7] + '---neblio-Qt---macOS.zip'

    nci.call_with_err_code('zip -j ' + file_name + ' ./neblio-Qt.dmg')
    nci.call_with_err_code('mv ' + file_name + ' ' + deploy_dir)
    nci.call_with_err_code('echo "Binary package at ' + deploy_dir + file_name + '"')
    # set the SOURCE_DIR env var, this points to the binary that will be uploaded
    nci.call_with_err_code('echo "::set-env name=SOURCE_PATH::' + deploy_dir + file_name + '"')

nci.call_with_err_code('ccache -s')

print("")
print("")
print("Building finished successfully.")
print("")
<|endoftext|>


<commit_before><commit_msg>delete music_crawler_test from main folder<commit_after><|endoftext|>


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

# Copyright 2018, Ross A. Beyer (rbeyer@seti.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This program is for dealing with .img files that come out of isis2pds and splitting them
# into detached labels for PDS3.

import os, sys, optparse
import pvl

def man(option, opt, value, parser):
    print >>sys.stderr, parser.usage
    print >>sys.stderr, '''\
This program works with PDS images and labels.
'''
    sys.exit()

class UsageError(Exception):
    def __init__(self, msg):
        self.msg = msg

def main():
    parser = optparse.OptionParser(usage="usage %prog [--help] [-o outname][-i isis2pds commands][-e] <file.img|file.cub>")
    parser.add_option("-o","--output", dest="outname", help="output will be written to FILE.lbl and FILE.img", metavar="FILE")
    parser.add_option("-i","--isis2pds", dest="isis2pds", help="string of options to pass to pds2isis")
    parser.add_option("-e", "--edit", action="store_true", dest="edit", default=False,
                  help="The program will attempt to edit the labels.")

    (options, args) = parser.parse_args()

    if not args: parser.error("need an .img or .cub file")

    imgfilename = args[0]

    (root, ext) = os.path.splitext( imgfilename )

    if( ext == '.cub' ):
        # Check for weird pixels that PDS doesn't want.
        statspvl = root+'_stats.pvl'
        statscmd = 'stats fr='+args[0]+' to= '+statspvl
        print( statscmd )
        os.system(statscmd)
        stats = pvl.load( statspvl )
        os.remove( statspvl )
        if( stats['Results']['LisPixels'] != 0 ): parser.error( args[0]+' has LisPixels!' )
        if( stats['Results']['LrsPixels'] != 0 ): parser.error( args[0]+' has LrsPixels!' )
        if( stats['Results']['HisPixels'] != 0 ): parser.error( args[0]+' has HisPixels!' )
        if( stats['Results']['HrsPixels'] != 0 ): parser.error( args[0]+' has HrsPixels!' )

        # Run isis2pds
        imgfilename = 'isis2pds.img'
        cmd = 'isis2pds fr= '+args[0]+' to= '+imgfilename
        if options.isis2pds: cmd += ' '+options.isis2pds
        print( cmd )
        os.system(cmd)

    label = pvl.load( imgfilename )

    label_file = root+'-det.lbl'
    data_file  = root+'-det.img'

    if options.outname:
        label_file = options.outname+'.lbl'
        data_file = options.outname+'.img'
        if( os.path.exists(label_file) ):
            parser.error(label_file+' exists!')
        if( os.path.exists(data_file) ):
            parser.error(data_file+' exists!')

    with open( imgfilename, 'rb') as infile:
        with open( label_file, 'wb') as outfile:
            ## outfile.write( infile.read( label['LABEL_RECORDS'].value ) )
            # It sure would be nice to perform this manipulation with the pvl library,
            # but it parses input values instead of preserving the bytes, so we'll have
            # to do this the heavy-handed way:
            label_lines = infile.read( label['LABEL_RECORDS'].value ).split(b'\r\n')
            for line in label_lines:
                #print( line )
                if( label['RECORD_TYPE'] == 'UNDEFINED' and line.startswith( b'RECORD_TYPE' ) ):
                   outfile.write( line.replace(b'UNDEFINED',b'FIXED_LENGTH')+b'\r\n' )
                   i = line.find(b'=')
                   outfile.write( b'FILE_RECORDS'.ljust(i)+b'= '+str(
                       label['IMAGE']['LINES'] ).encode('ascii') + b'\r\n' )
                   outfile.write( b'RECORD_BYTES'.ljust(i)+b'= '+ str(
                       int( (label['IMAGE']['SAMPLE_BITS'] / 8) * label['IMAGE']['LINE_SAMPLES'] )
                       ).encode('ascii') + b'\r\n' )
                elif( line.startswith( b'LABEL_RECORDS' ) ): continue
                elif( line.startswith( b'^IMAGE' ) ):
                    outfile.write( b'^IMAGE'.ljust(i)+b'= "'+os.path.basename(data_file).encode('ascii')+b'"\r\n' )
                elif( options.edit ):
                    if( label['IMAGE']['OFFSET'] != 0.0 and line.lstrip().startswith( b'OFFSET' ) ):
                        i = line.find(b'=')
                        outfile.write( b'OFFSET'.ljust(i)+b'= 0.0\r\n' )
                    elif( label['IMAGE']['SCALING_FACTOR'] != 1.0 and line.lstrip().startswith( b'SCALING_FACTOR' ) ):
                        i = line.find(b'=')
                        outfile.write( b'SCALING_FACTOR'.ljust(i)+b'= 1.0\r\n' )
                    elif( line.lstrip().startswith( b'CORE_NULL' ) ):
                        outfile.write( line.replace(b'CORE_NULL       ',b'MISSING_CONSTANT')+b'\r\n' )
                    elif( line.lstrip().startswith( b'CORE_LOW_REPR_SATURATION' ) ): continue
                    elif( line.lstrip().startswith( b'CORE_LOW_INSTR_SATURATION' ) ): continue
                    elif( line.lstrip().startswith( b'CORE_HIGH_REPR_SATURATION' ) ): continue
                    elif( line.lstrip().startswith( b'CORE_HIGH_INSTR_SATURATION' ) ): continue
                    else: outfile.write( line + b'\r\n' )

                else: outfile.write( line + b'\r\n' )

        with open( data_file, 'wb') as outfile:
            outfile.write( infile.read() )

    if( ext == '.cub' ): os.remove( imgfilename )

    #print( pvl.dumps( label ) )
    #print( str( pvl.dumps( label ), 'ascii') )

    # # label_file
    # outfile = 'label.lbl'
    # ascii_label = pvl.dumps(label)
    # with open( outfile, 'wb') as f:
    #     f.write(ascii_label)

if __name__ == "__main__":
    sys.exit(main())
<commit_msg>Added some spacing.<commit_after>#!/usr/bin/env python

# Copyright 2018, Ross A. Beyer (rbeyer@seti.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This program is for dealing with .img files that come out of isis2pds and splitting them
# into detached labels for PDS3.

import os, sys, optparse
import pvl

def man(option, opt, value, parser):
    print >>sys.stderr, parser.usage
    print >>sys.stderr, '''\
This program works with PDS images and labels.
'''
    sys.exit()

class UsageError(Exception):
    def __init__(self, msg):
        self.msg = msg

def main():
    parser = optparse.OptionParser(usage="usage %prog [--help] [-o outname][-i isis2pds commands][-e] <file.img|file.cub>")
    parser.add_option("-o","--output", dest="outname", help="output will be written to FILE.lbl and FILE.img", metavar="FILE")
    parser.add_option("-i","--isis2pds", dest="isis2pds", help="string of options to pass to pds2isis")
    parser.add_option("-e", "--edit", action="store_true", dest="edit", default=False,
                  help="The program will attempt to edit the labels.")

    (options, args) = parser.parse_args()

    if not args: parser.error("need an .img or .cub file")

    imgfilename = args[0]

    (root, ext) = os.path.splitext( imgfilename )

    if( ext == '.cub' ):
        # Check for weird pixels that PDS doesn't want.
        statspvl = root+'_stats.pvl'
        statscmd = 'stats fr='+args[0]+' to= '+statspvl
        print( statscmd )
        os.system(statscmd)
        stats = pvl.load( statspvl )
        os.remove( statspvl )
        if( stats['Results']['LisPixels'] != 0 ): parser.error( args[0]+' has LisPixels!' )
        if( stats['Results']['LrsPixels'] != 0 ): parser.error( args[0]+' has LrsPixels!' )
        if( stats['Results']['HisPixels'] != 0 ): parser.error( args[0]+' has HisPixels!' )
        if( stats['Results']['HrsPixels'] != 0 ): parser.error( args[0]+' has HrsPixels!' )

        # Run isis2pds
        imgfilename = 'isis2pds.img'
        cmd = 'isis2pds fr= '+args[0]+' to= '+imgfilename
        if options.isis2pds: cmd += ' '+options.isis2pds
        print( cmd )
        os.system(cmd)

    label = pvl.load( imgfilename )

    label_file = root+'-det.lbl'
    data_file  = root+'-det.img'

    if options.outname:
        label_file = options.outname+'.lbl'
        data_file = options.outname+'.img'
        if( os.path.exists(label_file) ):
            parser.error(label_file+' exists!')
        if( os.path.exists(data_file) ):
            parser.error(data_file+' exists!')

    with open( imgfilename, 'rb') as infile:
        with open( label_file, 'wb') as outfile:
            ## outfile.write( infile.read( label['LABEL_RECORDS'].value ) )
            # It sure would be nice to perform this manipulation with the pvl library,
            # but it parses input values instead of preserving the bytes, so we'll have
            # to do this the heavy-handed way:
            label_lines = infile.read( label['LABEL_RECORDS'].value ).split(b'\r\n')
            for line in label_lines:
                #print( line )
                if( label['RECORD_TYPE'] == 'UNDEFINED' and line.startswith( b'RECORD_TYPE' ) ):
                   outfile.write( line.replace(b'UNDEFINED',b'FIXED_LENGTH')+b'\r\n' )
                   i = line.find(b'=')
                   outfile.write( b'FILE_RECORDS'.ljust(i)+b'= '+str(
                       label['IMAGE']['LINES'] ).encode('ascii') + b'\r\n' )
                   outfile.write( b'RECORD_BYTES'.ljust(i)+b'= '+ str(
                       int( (label['IMAGE']['SAMPLE_BITS'] / 8) * label['IMAGE']['LINE_SAMPLES'] )
                       ).encode('ascii') + b'\r\n' )
                elif( line.startswith( b'LABEL_RECORDS' ) ): continue
                elif( line.startswith( b'^IMAGE' ) ):
                    outfile.write( b'^IMAGE'.ljust(i)+b'= "'+os.path.basename(data_file).encode('ascii')+b'"\r\n' )
                elif( options.edit ):
                    if( label['IMAGE']['OFFSET'] != 0.0 and line.lstrip().startswith( b'OFFSET' ) ):
                        i = line.find(b'=')
                        outfile.write( b'  OFFSET'.ljust(i)+b'= 0.0\r\n' )
                    elif( label['IMAGE']['SCALING_FACTOR'] != 1.0 and line.lstrip().startswith( b'SCALING_FACTOR' ) ):
                        i = line.find(b'=')
                        outfile.write( b'  SCALING_FACTOR'.ljust(i)+b'= 1.0\r\n' )
                    elif( line.lstrip().startswith( b'CORE_NULL' ) ):
                        outfile.write( line.replace(b'CORE_NULL       ',b'MISSING_CONSTANT')+b'\r\n' )
                    elif( line.lstrip().startswith( b'CORE_LOW_REPR_SATURATION' ) ): continue
                    elif( line.lstrip().startswith( b'CORE_LOW_INSTR_SATURATION' ) ): continue
                    elif( line.lstrip().startswith( b'CORE_HIGH_REPR_SATURATION' ) ): continue
                    elif( line.lstrip().startswith( b'CORE_HIGH_INSTR_SATURATION' ) ): continue
                    else: outfile.write( line + b'\r\n' )

                else: outfile.write( line + b'\r\n' )

        with open( data_file, 'wb') as outfile:
            outfile.write( infile.read() )

    if( ext == '.cub' ): os.remove( imgfilename )

    #print( pvl.dumps( label ) )
    #print( str( pvl.dumps( label ), 'ascii') )

    # # label_file
    # outfile = 'label.lbl'
    # ascii_label = pvl.dumps(label)
    # with open( outfile, 'wb') as f:
    #     f.write(ascii_label)

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


<commit_before>suite = {
    "mxversion" : "5.70.2",
    "name" : "java-llvm-ir-builder",
    "versionConflictResolution" : "latest",

    "imports" : {
        "suites" : [
            {
                "name" : "sulong",
                "version" : "c90bf5d7cba7b727377aeac1271751b880d14b13",
                "urls" : [
                    {
                        "url" : "https://github.com/pointhi/sulong",
                        "kind" : "git"
                    },
                ]
            },
        ],
    },

    "javac.lint.overrides" : "none",

    "projects" : {
        "at.pointhi.irbuilder.irwriter" : {
            "subDir" : "projects",
            "sourceDirs" : ["src"],
            "dependencies" : [
                "sulong:SULONG",
            ],
            "checkstyle" : "at.pointhi.irbuilder.irwriter",
            "javaCompliance" : "1.8",
            "license" : "BSD-new",
        },

        "at.pointhi.irbuilder.test": {
            "subDir": "projects",
            "sourceDirs": ["src"],
            "dependencies": [
                "at.pointhi.irbuilder.irwriter",
                "sulong:SULONG",
                "sulong:SULONG_TEST",
                "mx:JUNIT",
            ],
            "checkstyle": "at.pointhi.irbuilder.irwriter",
            "javaCompliance": "1.8",
            "license": "BSD-new",
        },
    },

    "distributions" : {
        "IRWRITER" : {
            "path" : "build/irwriter.jar",
            "subDir" : "graal",
            "sourcesPath" : "build/irbuilder.src.zip",
            "mainClass" : "at.pointhi.irbuilder.irwriter.SourceParser",
            "dependencies" : [
                "at.pointhi.irbuilder.irwriter"
            ],
            "distDependencies" : [
                "sulong:SULONG",
            ]
        },

        "IRWRITER_TEST" : {
            "path" : "build/irwriter_test.jar",
            "subDir" : "graal",
            "sourcesPath" : "build/irwriter_test.src.zip",
            "dependencies" : [
                "at.pointhi.irbuilder.test"
            ],
            "exclude" : [
                "mx:JUNIT"
            ],
            "distDependencies" : [
                "IRWRITER",
                "sulong:SULONG",
                "sulong:SULONG_TEST",
            ]
        },
    }
}
<commit_msg>Update sulong version<commit_after>suite = {
    "mxversion" : "5.70.2",
    "name" : "java-llvm-ir-builder",
    "versionConflictResolution" : "latest",

    "imports" : {
        "suites" : [
            {
                "name" : "sulong",
                "version" : "aeff0092c8b5c15fc8ae3f16bc48aa0318c14a3f",
                "urls" : [
                    {
                        "url" : "https://github.com/pointhi/sulong",
                        "kind" : "git"
                    },
                ]
            },
        ],
    },

    "javac.lint.overrides" : "none",

    "projects" : {
        "at.pointhi.irbuilder.irwriter" : {
            "subDir" : "projects",
            "sourceDirs" : ["src"],
            "dependencies" : [
                "sulong:SULONG",
            ],
            "checkstyle" : "at.pointhi.irbuilder.irwriter",
            "javaCompliance" : "1.8",
            "license" : "BSD-new",
        },

        "at.pointhi.irbuilder.test": {
            "subDir": "projects",
            "sourceDirs": ["src"],
            "dependencies": [
                "at.pointhi.irbuilder.irwriter",
                "sulong:SULONG",
                "sulong:SULONG_TEST",
                "mx:JUNIT",
            ],
            "checkstyle": "at.pointhi.irbuilder.irwriter",
            "javaCompliance": "1.8",
            "license": "BSD-new",
        },
    },

    "distributions" : {
        "IRWRITER" : {
            "path" : "build/irwriter.jar",
            "subDir" : "graal",
            "sourcesPath" : "build/irbuilder.src.zip",
            "mainClass" : "at.pointhi.irbuilder.irwriter.SourceParser",
            "dependencies" : [
                "at.pointhi.irbuilder.irwriter"
            ],
            "distDependencies" : [
                "sulong:SULONG",
            ]
        },

        "IRWRITER_TEST" : {
            "path" : "build/irwriter_test.jar",
            "subDir" : "graal",
            "sourcesPath" : "build/irwriter_test.src.zip",
            "dependencies" : [
                "at.pointhi.irbuilder.test"
            ],
            "exclude" : [
                "mx:JUNIT"
            ],
            "distDependencies" : [
                "IRWRITER",
                "sulong:SULONG",
                "sulong:SULONG_TEST",
            ]
        },
    }
}
<|endoftext|>


<commit_before>from UnitTest import UnitTest

class ListTest(UnitTest):

    def testSliceGet(self):
        value = [0, 1, 2, 3, 4]

        self.assertTrue(value[-1] is 4)
        self.assertTrue(value[1] is 1)
        self.assertTrue(value[4] is 4)
        self.assertTrue(value[-3] is 2)

    def testSliceRange(self):
        value = [0, 1, 2, 3, 4]

        self.assertTrue(value[1:3][0] is 1)
        self.assertTrue(value[1:3][1] is 2)
        self.assertTrue(len(value[1:2]) is 1)
        self.assertTrue(len(value[1:3]) is 2)

        self.assertTrue(value[:2][0] is 0)
        self.assertTrue(value[:2][1] is 1)
        self.assertTrue(len(value[:2]) is 2)
        self.assertTrue(len(value[:1]) is 1)

        self.assertTrue(value[:-1][0] is 0)
        self.assertTrue(value[:-1][3] is 3)
        self.assertTrue(len(value[:-1]) is 4)

        self.assertTrue(value[:][3] is 3)
        self.assertTrue(len(value[:]) is 5)

        self.assertTrue(value[0:][3] is 3)
        self.assertTrue(value[1:][0] is 1)
        self.assertTrue(len(value[1:]) is 4)

        self.assertTrue(value[-1:][0] is 4)
        self.assertTrue(len(value[-1:3]) is 0)

    def testDelete(self):
        value = [0, 1, 2, 3, 4]
        del value[4]
        self.assertTrue(len(value) is 4)
        self.assertTrue(value[3] is 3)

        del value[-1]
        self.assertTrue(len(value) is 3)
        self.assertTrue(value[2] is 2)

    def testPop(self):
        a = ['a']
        b = ['b']
        c = ['c']
        d = ['d']
        e = ['e']

        value = [a, b, c, d, e]

        x = value.pop(4)
        self.assertTrue(x==e)
        self.assertTrue(len(value) is 4)

        x = value.pop(-1)
        self.assertTrue(x==d)
        self.assertTrue(len(value) is 3)

        x = value.pop()
        self.assertTrue(x==c)
        self.assertTrue(len(value) is 2)

        x = value.pop(0)
        self.assertTrue(x==a)
        self.assertTrue(len(value) is 1)

    def testSort(self):
        l1 = ['c', 'd', 'a', 'b']
        l1.sort()
        self.assertTrue(l1[0] == 'a')
        self.assertTrue(l1[1] == 'b')
        self.assertTrue(l1[2] == 'c')
        self.assertTrue(l1[3] == 'd')

        l2 = ['C', 'd', 'A', 'b']
        def toLower(x):
            return x.lower()
        l2.sort(None, toLower)
        self.assertTrue(l2[0] == 'A')
        self.assertTrue(l2[1] == 'b')
        self.assertTrue(l2[2] == 'C')
        self.assertTrue(l2[3] == 'd')

        l3 = ['C', 'd', 'A', 'b']
        l3.sort(None, toLower, True)
        self.assertTrue(l3[0] == 'd')
        self.assertTrue(l3[1] == 'C')
        self.assertTrue(l3[2] == 'b')
        self.assertTrue(l3[3] == 'A')

        l4 = ['c', 'd', 'a', 'b']
        l4.sort(None, None, True)
        self.assertTrue(l4[0] == 'd')
        self.assertTrue(l4[1] == 'c')
        self.assertTrue(l4[2] == 'b')
        self.assertTrue(l4[3] == 'a')

    def testCmp(self):
        l1 = [1,2,3]
        l2 = [1,2]
        l3 = [1,2,3]
        l4 = [1,2,4]

        t1 = (1,2,3)

        self.assertEquals(cmp([],[]), 0, "Empty lists are the same")
        self.assertTrue([]==[])
        self.assertEquals([]!=[], False)
        self.assertTrue(cmp(l1, l2) == 1)
        self.assertTrue(cmp(l2, l1) == -1)
        self.assertTrue(cmp(l3, l4) == -1)
        self.assertTrue(cmp(l4, l3) == 1)
        self.assertTrue(l1 == l3, "l1 == l3")
        self.assertTrue(l1 > l2, "l1 > l2")
        self.assertTrue(l1 >= l2, "l1 >= l2")
        self.assertTrue(l2 < l1, "l2 < l1")
        self.assertTrue(l2 <= l1, "l2 <= l1")

    def testCmpListTuple(self):
        t1 = (1,2,3)
        l1 = [1,2,3]

        self.assertFalse(l1 == t1)
        self.assertTrue(cmp(l1, t1) == -1)
        self.assertTrue(cmp(t1, l1) == 1)
        self.assertTrue(l1 != t1, "l1 != t1")

    def testSortCmp(self):
        a = A()
        l1 = [a, 1]
        l1.sort()
        l2 = [1, a]
        l2.sort()
        self.assertTrue(l1[0] is a) # don't use == it will call A.__cmp__!
        self.assertTrue(l2[0] is a) # don't use == it will call A.__cmp__!
        self.assertFalse(l1[0] == a) # use == A.__cmp__ always fails

    def testReverse(self):
        l = [1,2,3]
        l.reverse()
        self.assertEqual(l[0], 3)
        self.assertEqual(l[2], 1)

    def testConstructor(self):
        l1 = list()
        self.assertEqual(len(l1),0)

        # only accept list or iterator
        l2 = list()
        self.assertEqual(len(l2),0)

        l3 = list([])
        self.assertEqual(len(l3),0)

        l4 = list([10,])
        self.assertEqual(len(l4),1)
        self.assertEqual(l4[0],10)

        l5 = list(range(10,40,10))
        self.assertEqual(len(l5),3)
        self.assertEqual(l5[0],10)
        self.assertEqual(l5[1],20)
        self.assertEqual(l5[2],30)

        l6 = list(l4)
        self.assertEqual(len(l6),1)
        self.assertEqual(l6[0],10)

    def testExtend(self):
        l = [10,20]
        l.extend([30,40])
        self.assertEqual(len(l),4)
        self.assertEqual(l[0], 10)
        self.assertEqual(l[1], 20)
        self.assertEqual(l[2], 30)
        self.assertEqual(l[3], 40)

        l2 = [10,20]
        l2.extend([])
        self.assertEqual(len(l2),2)

        l3 = []
        l3.extend([10,20])
        self.assertEqual(len(l3),2)
        self.assertEqual(l3[0],10)
        self.assertEqual(l3[1],20)

        l4 = []
        l4.extend([])
        self.assertEqual(len(l4),0)

    def testIter2(self):
        i = 0

        for item in [0,1,2,3]:
            self.assertEqual(item, i)
            i += 1

        i = 0
        for item in [0,1,2,3][1:-1]:
            i += item
        self.assertEqual(i, 3)

    def testIter(self):
        l = [0,1,2,3]
        i = 0

        it = l.__iter__()
        while True:
            try:
                item = it.next()
            except StopIteration:
                break
            self.assertEqual(item, l[i])
            i += 1

    def testIndex(self):
        l = [2,4,6,8]
        try:
            self.assertEqual(l.index(2), 0)
        except ValueError:
            self.fail("ValueError raised when not expected")

        try:
            l.index(200000)
        except ValueError, e:
            self.assertTrue(str(e) == "list.index(x): x not in list",
                            "ValueError exception has incorrect message")
        else:
            self.fail("ValueError not raised")

    def testAugAssign(self):
        l = [10, 10.0]
        def getidx(x):
            return x
        def getlist():
            return l
        l[0] += 1
        self.assertEqual(l[0], 11)
        l[0] -= 2
        self.assertEqual(l[0], 9)
        l[0] /= 3
        self.assertEqual(l[0], 3)
        l[0] *= 9
        self.assertEqual(l[0], 27)
        l[0] %= 5
        self.assertEqual(l[0], 2)
        a = 0
        l[a] += 1
        self.assertEqual(l[0], 3)
        l[getidx(0)] += 1
        self.assertEqual(l[0], 4)
        getlist()[getidx(0)] += 1
        self.assertEqual(l[0], 5)

class A:
    def __cmp__(self, other):
        return -1

<commit_msg>added a test case that does not work TODO: implement it<commit_after>from UnitTest import UnitTest

class ListTest(UnitTest):

    def testSliceGet(self):
        value = [0, 1, 2, 3, 4]

        self.assertTrue(value[-1] is 4)
        self.assertTrue(value[1] is 1)
        self.assertTrue(value[4] is 4)
        self.assertTrue(value[-3] is 2)

    def testSliceRange(self):
        value = [0, 1, 2, 3, 4]

        self.assertTrue(value[1:3][0] is 1)
        self.assertTrue(value[1:3][1] is 2)
        self.assertTrue(len(value[1:2]) is 1)
        self.assertTrue(len(value[1:3]) is 2)

        self.assertTrue(value[:2][0] is 0)
        self.assertTrue(value[:2][1] is 1)
        self.assertTrue(len(value[:2]) is 2)
        self.assertTrue(len(value[:1]) is 1)

        self.assertTrue(value[:-1][0] is 0)
        self.assertTrue(value[:-1][3] is 3)
        self.assertTrue(len(value[:-1]) is 4)

        self.assertTrue(value[:][3] is 3)
        self.assertTrue(len(value[:]) is 5)

        self.assertTrue(value[0:][3] is 3)
        self.assertTrue(value[1:][0] is 1)
        self.assertTrue(len(value[1:]) is 4)

        self.assertTrue(value[-1:][0] is 4)
        self.assertTrue(len(value[-1:3]) is 0)

    def testDelete(self):
        value = [0, 1, 2, 3, 4]
        del value[4]
        self.assertTrue(len(value) is 4)
        self.assertTrue(value[3] is 3)

        del value[-1]
        self.assertTrue(len(value) is 3)
        self.assertTrue(value[2] is 2)

    def testPop(self):
        a = ['a']
        b = ['b']
        c = ['c']
        d = ['d']
        e = ['e']

        value = [a, b, c, d, e]

        x = value.pop(4)
        self.assertTrue(x==e)
        self.assertTrue(len(value) is 4)

        x = value.pop(-1)
        self.assertTrue(x==d)
        self.assertTrue(len(value) is 3)

        x = value.pop()
        self.assertTrue(x==c)
        self.assertTrue(len(value) is 2)

        x = value.pop(0)
        self.assertTrue(x==a)
        self.assertTrue(len(value) is 1)

    def testSort(self):
        l1 = ['c', 'd', 'a', 'b']
        l1.sort()
        self.assertTrue(l1[0] == 'a')
        self.assertTrue(l1[1] == 'b')
        self.assertTrue(l1[2] == 'c')
        self.assertTrue(l1[3] == 'd')

        l2 = ['C', 'd', 'A', 'b']
        def toLower(x):
            return x.lower()
        l2.sort(None, toLower)
        self.assertTrue(l2[0] == 'A')
        self.assertTrue(l2[1] == 'b')
        self.assertTrue(l2[2] == 'C')
        self.assertTrue(l2[3] == 'd')

        l3 = ['C', 'd', 'A', 'b']
        l3.sort(None, toLower, True)
        self.assertTrue(l3[0] == 'd')
        self.assertTrue(l3[1] == 'C')
        self.assertTrue(l3[2] == 'b')
        self.assertTrue(l3[3] == 'A')

        l4 = ['c', 'd', 'a', 'b']
        l4.sort(None, None, True)
        self.assertTrue(l4[0] == 'd')
        self.assertTrue(l4[1] == 'c')
        self.assertTrue(l4[2] == 'b')
        self.assertTrue(l4[3] == 'a')

    def testCmp(self):
        l1 = [1,2,3]
        l2 = [1,2]
        l3 = [1,2,3]
        l4 = [1,2,4]

        t1 = (1,2,3)

        self.assertEquals(cmp([],[]), 0, "Empty lists are the same")
        self.assertTrue([]==[])
        self.assertEquals([]!=[], False)
        self.assertTrue(cmp(l1, l2) == 1)
        self.assertTrue(cmp(l2, l1) == -1)
        self.assertTrue(cmp(l3, l4) == -1)
        self.assertTrue(cmp(l4, l3) == 1)
        self.assertTrue(l1 == l3, "l1 == l3")
        self.assertTrue(l1 > l2, "l1 > l2")
        self.assertTrue(l1 >= l2, "l1 >= l2")
        self.assertTrue(l2 < l1, "l2 < l1")
        self.assertTrue(l2 <= l1, "l2 <= l1")

        b1 = B()
        b2 = B()
        l1 = [b1, b2]
        l2 = [b2, b1]
        self.assertFalse(l1==l2,
                         'TODO: List comparison with plain objects not implemented')

    def testCmpListTuple(self):
        t1 = (1,2,3)
        l1 = [1,2,3]

        self.assertFalse(l1 == t1)
        self.assertTrue(cmp(l1, t1) == -1)
        self.assertTrue(cmp(t1, l1) == 1)
        self.assertTrue(l1 != t1, "l1 != t1")

    def testSortCmp(self):
        a = A()
        l1 = [a, 1]
        l1.sort()
        l2 = [1, a]
        l2.sort()
        self.assertTrue(l1[0] is a) # don't use == it will call A.__cmp__!
        self.assertTrue(l2[0] is a) # don't use == it will call A.__cmp__!
        self.assertFalse(l1[0] == a) # use == A.__cmp__ always fails

    def testReverse(self):
        l = [1,2,3]
        l.reverse()
        self.assertEqual(l[0], 3)
        self.assertEqual(l[2], 1)

    def testConstructor(self):
        l1 = list()
        self.assertEqual(len(l1),0)

        # only accept list or iterator
        l2 = list()
        self.assertEqual(len(l2),0)

        l3 = list([])
        self.assertEqual(len(l3),0)

        l4 = list([10,])
        self.assertEqual(len(l4),1)
        self.assertEqual(l4[0],10)

        l5 = list(range(10,40,10))
        self.assertEqual(len(l5),3)
        self.assertEqual(l5[0],10)
        self.assertEqual(l5[1],20)
        self.assertEqual(l5[2],30)

        l6 = list(l4)
        self.assertEqual(len(l6),1)
        self.assertEqual(l6[0],10)

    def testExtend(self):
        l = [10,20]
        l.extend([30,40])
        self.assertEqual(len(l),4)
        self.assertEqual(l[0], 10)
        self.assertEqual(l[1], 20)
        self.assertEqual(l[2], 30)
        self.assertEqual(l[3], 40)

        l2 = [10,20]
        l2.extend([])
        self.assertEqual(len(l2),2)

        l3 = []
        l3.extend([10,20])
        self.assertEqual(len(l3),2)
        self.assertEqual(l3[0],10)
        self.assertEqual(l3[1],20)

        l4 = []
        l4.extend([])
        self.assertEqual(len(l4),0)

    def testIter2(self):
        i = 0

        for item in [0,1,2,3]:
            self.assertEqual(item, i)
            i += 1

        i = 0
        for item in [0,1,2,3][1:-1]:
            i += item
        self.assertEqual(i, 3)

    def testIter(self):
        l = [0,1,2,3]
        i = 0

        it = l.__iter__()
        while True:
            try:
                item = it.next()
            except StopIteration:
                break
            self.assertEqual(item, l[i])
            i += 1

    def testIndex(self):
        l = [2,4,6,8]
        try:
            self.assertEqual(l.index(2), 0)
        except ValueError:
            self.fail("ValueError raised when not expected")

        try:
            l.index(200000)
        except ValueError, e:
            self.assertTrue(str(e) == "list.index(x): x not in list",
                            "ValueError exception has incorrect message")
        else:
            self.fail("ValueError not raised")

    def testAugAssign(self):
        l = [10, 10.0]
        def getidx(x):
            return x
        def getlist():
            return l
        l[0] += 1
        self.assertEqual(l[0], 11)
        l[0] -= 2
        self.assertEqual(l[0], 9)
        l[0] /= 3
        self.assertEqual(l[0], 3)
        l[0] *= 9
        self.assertEqual(l[0], 27)
        l[0] %= 5
        self.assertEqual(l[0], 2)
        a = 0
        l[a] += 1
        self.assertEqual(l[0], 3)
        l[getidx(0)] += 1
        self.assertEqual(l[0], 4)
        getlist()[getidx(0)] += 1
        self.assertEqual(l[0], 5)

class A:
    def __cmp__(self, other):
        return -1

class B:
    pass

<|endoftext|>


<commit_before>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# PCEF - Python/Qt Code Editing Framework
# Copyright 2013, Colin Duquesnoy <colin.duquesnoy@gmail.com>
#
# This software is released under the LGPLv3 license.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Contains the mode that control the external changes of file.
"""
import os
from pcef.core.mode import Mode
from pcef.qt import QtCore, QtGui

class FileWatcher(Mode):

    """
    FileWatcher mode. (Verify the external changes from opened file)
    """
    #: Mode identifier
    IDENTIFIER = "FileWatcher"
    #: Mode description
    DESCRIPTION = "Verify if file is modified externally."

    def __init__(self):
        super(FileWatcher, self).__init__()
        self.__filesystemwatcher = QtCore.QFileSystemWatcher()
        self.__flgNotify = False
        self.__changeWaiting = False
        self.__filesystemwatcher.fileChanged.connect(self.__onFileChanged)

    def __notifyChange(self):
        self.__flgNotify = True
        auto = self.editor.settings.value("autoReloadChangedFiles")
        if (auto or QtGui.QMessageBox.question(
                self.editor, "File changed",
                "The file %s has has changed externally.\n"
                "Do you want reload it?" % os.path.basename(
                        self.editor.filePath),
                QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) ==
                QtGui.QMessageBox.Yes):
            self.editor.openFile(self.editor.filePath)
        self.__changeWaiting = False
        self.__flgNotify = False

    def __onFileChanged(self):
        self.__changeWaiting = True
        if self.editor.hasFocus() and self.__flgNotify:
            self.__notifyChange()

    @QtCore.Slot()
    def __onEditorFilePathChanged(self):
        path = self.editor.filePath
        if path not in self.__filesystemwatcher.files():
            self.__filesystemwatcher.addPath(path)

    @QtCore.Slot()
    def __onEditorFocusIn(self):
        if self.__changeWaiting:
            self.__notifyChange()

    def install(self, editor):
        Mode.install(self, editor)
        self.editor.settings.addProperty("autoReloadChangedFiles", False)

    def onStateChanged(self, state):
        """
        Connects/Disconnects to the mouseWheelActivated and keyPressed event
        """
        if state is True:
            self.editor.textSaved.connect(self.__onEditorFilePathChanged)
            self.editor.newTextSet.connect(self.__onEditorFilePathChanged)
            self.editor.focusedIn.connect(self.__onEditorFocusIn)
        else:
            self.editor.textSaved.disconnect(self.__onEditorFilePathChanged)
            self.editor.newTextSet.disconnect(self.__onEditorFilePathChanged)
            self.editor.focusedIn.disconnect(self.__onEditorFocusIn)
            self.__filesystemwatcher.removePath(self.editor.filePath)

if __name__ == '__main__':
    from pcef.core import QGenericCodeEdit, TextDecoration

    class Example(QGenericCodeEdit):

        def __init__(self):
            QGenericCodeEdit.__init__(self, parent=None)
            self.installMode(FileWatcher())
            self.openFile(__file__)
            self.resize(QtCore.QSize(1000, 600))

        def closeEvent(self, evt):
            print("closing")
            self.uninstallMode(FileWatcher)

    import sys
    app = QtGui.QApplication(sys.argv)
    e = Example()
    e.show()
    sys.exit(app.exec_())
<commit_msg>Cleanup + good IDENTIFIER #49<commit_after>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# PCEF - Python/Qt Code Editing Framework
# Copyright 2013, Colin Duquesnoy <colin.duquesnoy@gmail.com>
#
# This software is released under the LGPLv3 license.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Contains the mode that control the external changes of file.
"""
import os
from pcef.core.mode import Mode
from pcef.qt import QtCore, QtGui

class FileWatcherMode(Mode):

    """
    FileWatcher mode. (Verify the external changes from opened file)
    """
    #: Mode identifier
    IDENTIFIER = "fileWatcherMode"
    #: Mode description
    DESCRIPTION = "Watch the editor's file and take care of the reloading."

    def __init__(self):
        super(FileWatcherMode, self).__init__()
        self.__fileSystemWatcher = QtCore.QFileSystemWatcher()
        self.__flgNotify = False
        self.__changeWaiting = False
        self.__fileSystemWatcher.fileChanged.connect(self.__onFileChanged)

    def __notifyChange(self):
        self.__flgNotify = True
        auto = self.editor.settings.value("autoReloadChangedFiles")
        if (auto or QtGui.QMessageBox.question(
                self.editor, "File changed",
                "The file %s has has changed externally.\n"
                "Do you want reload it?" % os.path.basename(
                        self.editor.filePath),
                QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) ==
                QtGui.QMessageBox.Yes):
            self.editor.openFile(self.editor.filePath)
        self.__changeWaiting = False
        self.__flgNotify = False

    def __onFileChanged(self):
        self.__changeWaiting = True
        if self.editor.hasFocus() and self.__flgNotify:
            self.__notifyChange()

    @QtCore.Slot()
    def __onEditorFilePathChanged(self):
        path = self.editor.filePath
        if path not in self.__fileSystemWatcher.files():
            self.__fileSystemWatcher.addPath(path)

    @QtCore.Slot()
    def __onEditorFocusIn(self):
        if self.__changeWaiting:
            self.__notifyChange()

    def install(self, editor):
        """
        Adds autoReloadChangedFiles settings on install.
        """
        Mode.install(self, editor)
        self.editor.settings.addProperty("autoReloadChangedFiles", False)

    def onStateChanged(self, state):
        """
        Connects/Disconnects to the mouseWheelActivated and keyPressed event
        """
        if state is True:
            self.editor.textSaved.connect(self.__onEditorFilePathChanged)
            self.editor.newTextSet.connect(self.__onEditorFilePathChanged)
            self.editor.focusedIn.connect(self.__onEditorFocusIn)
        else:
            self.editor.textSaved.disconnect(self.__onEditorFilePathChanged)
            self.editor.newTextSet.disconnect(self.__onEditorFilePathChanged)
            self.editor.focusedIn.disconnect(self.__onEditorFocusIn)
            self.__fileSystemWatcher.removePath(self.editor.filePath)

if __name__ == '__main__':
    from pcef.core import QGenericCodeEdit

    class Example(QGenericCodeEdit):

        def __init__(self):
            QGenericCodeEdit.__init__(self, parent=None)
            self.installMode(FileWatcherMode())
            self.openFile(__file__)
            self.resize(QtCore.QSize(1000, 600))

        def closeEvent(self, evt):
            print("closing")
            self.uninstallMode(FileWatcherMode)

    import sys
    app = QtGui.QApplication(sys.argv)
    e = Example()
    e.show()
    sys.exit(app.exec_())
<|endoftext|>