<commit_before>#!/usr/bin/python

# @file dlnap.py
# @author cherezov.pavel@gmail.com
# @brief Python over the network media player to playback on DLNA UPnP devices.

# Change log:
#   0.1 Initial version
#   0.2 Device renamed to DlnapDevice; DLNAPlayer is disappeared.
#   0.3 Debug output is added. Extract location url fixed.

__version__ = "0.3"

import re
import sys
import time
import socket
import select
from contextlib import contextmanager

py3 = sys.version_info[0] == 3
if py3:
   from urllib.request import urlopen
else:
   from urllib2 import urlopen

SSDP_GROUP = ("239.255.255.250", 1900)
URN_AVTransport = "urn:schemas-upnp-org:service:AVTransport:1"

def _get_port(location):
   """ Extract port number from url.

   location -- string like http://anyurl:port/whatever/path
   return -- port number
   """
   port = re.findall('http://.*:(\d+).*', location)
   return int(port[0]) if port else 80

def _get_control_url(raw):
   """ Extract AVTransport contol url from raw device description xml

   raw -- raw device description xml
   return -- control url or empty string if wasn't found
   """
   url = re.findall('\<serviceType\>{}\</serviceType\>.*\<controlURL\>(.*)\</controlURL\>'.format(re.escape(URN_AVTransport)), raw.replace('\n', ''))
   return url[0] if url else ''

@contextmanager
def _send_udp(to, payload):
   """ Send UDP mesage to group

   to -- (host, port) group to send to payload to
   payload -- message to send
   """
   sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
   sock.sendto(payload.encode(), to)
   yield sock
   sock.close()

def _send_tcp(to, payload):
   """ Send TCP mesage to group

   to -- (host, port) group to send to payload to
   payload -- message to send
   """
   try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.connect(to)
      sock.sendall(payload.encode())
   finally:
      sock.close()

def _get_location_url(raw):
   """ Extract device description url from discovery response

   raw -- raw discovery response
   return -- location url string
   """
   for d in raw.split('\r\n'):
      d = d.lower()
      if d.startswith('location:'):
         return d.replace('location:', '').strip()
   return ''

def _get_friendly_name(raw):
   """ Extract device name from raw description xml

   raw -- device description xml
   return -- device name
   """
   name = re.findall('\<friendlyName\>(.*)\</friendlyName\>', raw)
   return name[0] if name else 'Unknown'

class DlnapDevice:
   """ Represents DLNA device.
   """

   def __init__(self, raw, ip, debug=False):
      self.__raw = raw.decode()
      self.ip = ip
      self.port = None
      self.control_url = None
      self.name = None
      self.has_av_transport = False
      self.debug = debug

      try:
         self.location = _get_location_url(self.__raw)
         self.port = _get_port(self.location)
         self.__desc_xml = urlopen(self.location).read().decode()
         self.name = _get_friendly_name(self.__desc_xml)
         self.has_av_transport = '<serviceType>{}</serviceType>'.format(URN_AVTransport) in self.__desc_xml
         self.control_url = _get_control_url(self.__desc_xml)
      except Exception as e:
         if self.debug:
            print(e)
            print(self.ip)
            print(self.location)

   def __repr__(self):
      return '{} @ {}'.format(self.name, self.ip)

   def __eq__(self, d):
      return self.name == d.name and self.ip == d.ip

   def _set_av(self, url, instance_id = 0):
      payload = """<?xml version="1.0" encoding="utf-8"?>
         <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <s:Body>
               <u:SetAVTransportURI xmlns:u="{}">
                  <InstanceID>{}</InstanceID>
                  <CurrentURI>{}</CurrentURI>
                  <CurrentURIMetaData />
               </u:SetAVTransportURI>
            </s:Body>
         </s:Envelope>""".format(URN_AVTransport, instance_id, url)

      header = "\r\n".join([
         'POST {} HTTP/1.1'.format(self.control_url),
         'User-Agent: dlnap/0.1',
         'Accept: */*',
         'Content-Type: text/xml; charset="utf-8"',
         'HOST: {}:{}'.format(self.ip, self.port),
         'Content-Length: {}'.format(len(payload)),
         'SOAPACTION: "{}#SetAVTransportURI"'.format(URN_AVTransport),
         'Connection: close',
         '',
         payload,
         ])
      _send_tcp((self.ip, self.port), header)

   def _play(self, instance_id=0):
      payload = """<?xml version="1.0" encoding="utf-8"?>
         <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <s:Body>
               <u:Play xmlns:u="{}">
                  <InstanceID>{}</InstanceID>
                  <Speed>1</Speed>
               </u:Play>
            </s:Body>
         </s:Envelope>""".format(URN_AVTransport, instance_id)
      header = "\r\n".join([
           'POST {} HTTP/1.1'.format(self.control_url),
           'User-Agent: dlnap/0.1',
           'Accept: */*',
           'Content-Type: text/xml; charset="utf-8"',
           'HOST: {}:{}'.format(self.ip, self.port),
           'Content-Length: {}'.format(len(payload)),
           'SOAPACTION: "{}#Play"'.format(URN_AVTransport),
           'Connection: close',
           '',
           payload
           ])
      _send_tcp((self.ip, self.port), header)

   def play(self, url):
      self._set_av(url)
      self._play()

def discover(name = '', timeout = 1, st = "ssdp:all", mx = 3, debug = False):
   """ Discover UPnP devices in the local network.

   name -- name or part of the name to filter devices
   timeout -- timeout to perform discover
   st -- st field of discovery packet
   mx -- mx field of discovery packet
   debug -- True if debug output is required
   return -- list of DlnapDevice
   """
   payload = "\r\n".join([
              'M-SEARCH * HTTP/1.1',
              'HOST: {}:{}'.format(*SSDP_GROUP),
              'Accept: */*',
              'MAN: "ssdp:discover"',
              'ST: {}'.format(st),
              'MX: {}'.format(mx),
              '',
              ''])
   devices = []
   with _send_udp(SSDP_GROUP, payload) as sock:
      start = time.time()
      while True:
         if time.time() - start > timeout:
            # timedout
            break
         r, w, x = select.select([sock], [], [sock], 1)
         if sock in r:
             data, addr = sock.recvfrom(1024)
             d = DlnapDevice(data, addr[0], debug=debug)
             if d not in devices:
                if not name or name is None or name in d.name:
                   devices.append(d)
         elif sock in x:
             raise Exception('Getting response failed')
         else:
             # Nothing to read
             pass
   return devices

if __name__ == '__main__':
   import getopt

   def usage():
      print('dlna.py [--list] [-d[evice] <name>] [-t[imeout] <seconds>] [--play <url>]')

   try:
      opts, args = getopt.getopt(sys.argv[1:], "hvd:t:", ['help', 'version', 'debug', 'play=', 'pause', 'stop', 'list', 'device=', 'timeout='])
   except getopt.GetoptError:
      usage()
      sys.exit(1)

   device = ''
   url = ''
   timeout = 0.5
   action = ''
   debug = False
   for opt, arg in opts:
      if opt in ('-h', '--help'):
         usage()
         sys.exit(0)
      if opt in ('-v', '--version'):
         print(__version__)
         sys.exit(0)
      if opt in ('--debug'):
         debug = True
      elif opt in ('-d', '--device'):
         device = arg
      elif opt in ('-t', '--timeout'):
         timeout = float(arg)
      if opt in ('--list'):
         action = 'list'
      elif opt in ('--play'):
         action = 'play'
         url = arg
      elif opt in ('--pause'):
         action = 'pause'
      elif opt in ('--stop'):
         action = 'stop'

   allDevices = discover(name=device, timeout=timeout, debug=debug)
   if not allDevices:
      print('No devices found.')
      sys.exit(1)

   if action in ('', 'list'):
      print('Discovered devices:')
      for d in allDevices:
         print(' {} {}'.format('+' if d.has_av_transport else '-', d))
      sys.exit(0)

   d = allDevices[0]
   print(d)
   if action == 'play':
      d.play(url=url)
   elif action == 'pause':
      d.pause()
   elif action == 'stop':
      d.stop()
<commit_msg>Minor fixes<commit_after>#!/usr/bin/python

# @file dlnap.py
# @author cherezov.pavel@gmail.com
# @brief Python over the network media player to playback on DLNA UPnP devices.

# Change log:
#   0.1 Initial version
#   0.2 Device renamed to DlnapDevice; DLNAPlayer is disappeared.
#   0.3 Debug output is added. Extract location url fixed.

__version__ = "0.3"

import re
import sys
import time
import socket
import select
from contextlib import contextmanager

py3 = sys.version_info[0] == 3
if py3:
   from urllib.request import urlopen
else:
   from urllib2 import urlopen

SSDP_GROUP = ("239.255.255.250", 1900)
URN_AVTransport = "urn:schemas-upnp-org:service:AVTransport:1"

def _get_port(location):
   """ Extract port number from url.

   location -- string like http://anyurl:port/whatever/path
   return -- port number
   """
   port = re.findall('http://.*:(\d+).*', location)
   return int(port[0]) if port else 80

def _get_control_url(raw):
   """ Extract AVTransport contol url from raw device description xml

   raw -- raw device description xml
   return -- control url or empty string if wasn't found
   """
   url = re.findall('\<serviceType\>{}\</serviceType\>.*\<controlURL\>(.*)\</controlURL\>'.format(re.escape(URN_AVTransport)), raw.replace('\n', ''))
   return url[0] if url else ''

@contextmanager
def _send_udp(to, payload):
   """ Send UDP mesage to group

   to -- (host, port) group to send to payload to
   payload -- message to send
   """
   sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
   sock.sendto(payload.encode(), to)
   yield sock
   sock.close()

def _send_tcp(to, payload):
   """ Send TCP mesage to group

   to -- (host, port) group to send to payload to
   payload -- message to send
   """
   try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.connect(to)
      sock.sendall(payload.encode())
   finally:
      sock.close()

def _get_location_url(raw):
   """ Extract device description url from discovery response

   raw -- raw discovery response
   return -- location url string
   """
   for d in raw.split('\r\n'):
      d = d.lower()
      if d.startswith('location:'):
         return d.replace('location:', '').strip()
   return ''

def _get_friendly_name(raw):
   """ Extract device name from raw description xml

   raw -- device description xml
   return -- device name
   """
   name = re.findall('\<friendlyName\>(.*)\</friendlyName\>', raw)
   return name[0] if name else 'Unknown'

class DlnapDevice:
   """ Represents DLNA device.
   """

   def __init__(self, raw, ip, debug=False):
      self.__raw = raw.decode()
      self.ip = ip
      self.port = None
      self.control_url = None
      self.name = 'Unknown'
      self.has_av_transport = False
      self.debug = debug

      try:
         self.location = _get_location_url(self.__raw)
         self.port = _get_port(self.location)
         self.__desc_xml = urlopen(self.location).read().decode()
         self.name = _get_friendly_name(self.__desc_xml)
         self.has_av_transport = '<serviceType>{}</serviceType>'.format(URN_AVTransport) in self.__desc_xml
         self.control_url = _get_control_url(self.__desc_xml)
      except Exception as e:
         if self.debug:
            print(e)
            print(self.ip)
            print(self.location)

   def __repr__(self):
      return '{} @ {}'.format(self.name, self.ip)

   def __eq__(self, d):
      return self.name == d.name and self.ip == d.ip

   def set_current(self, url, instance_id = 0):
      payload = """<?xml version="1.0" encoding="utf-8"?>
         <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <s:Body>
               <u:SetAVTransportURI xmlns:u="{}">
                  <InstanceID>{}</InstanceID>
                  <CurrentURI>{}</CurrentURI>
                  <CurrentURIMetaData />
               </u:SetAVTransportURI>
            </s:Body>
         </s:Envelope>""".format(URN_AVTransport, instance_id, url)

      header = "\r\n".join([
         'POST {} HTTP/1.1'.format(self.control_url),
         'User-Agent: dlnap/0.1',
         'Accept: */*',
         'Content-Type: text/xml; charset="utf-8"',
         'HOST: {}:{}'.format(self.ip, self.port),
         'Content-Length: {}'.format(len(payload)),
         'SOAPACTION: "{}#SetAVTransportURI"'.format(URN_AVTransport),
         'Connection: close',
         '',
         payload,
         ])
      _send_tcp((self.ip, self.port), header)

   def play(self, instance_id=0):
      payload = """<?xml version="1.0" encoding="utf-8"?>
         <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <s:Body>
               <u:Play xmlns:u="{}">
                  <InstanceID>{}</InstanceID>
                  <Speed>1</Speed>
               </u:Play>
            </s:Body>
         </s:Envelope>""".format(URN_AVTransport, instance_id)
      header = "\r\n".join([
           'POST {} HTTP/1.1'.format(self.control_url),
           'User-Agent: dlnap/0.1',
           'Accept: */*',
           'Content-Type: text/xml; charset="utf-8"',
           'HOST: {}:{}'.format(self.ip, self.port),
           'Content-Length: {}'.format(len(payload)),
           'SOAPACTION: "{}#Play"'.format(URN_AVTransport),
           'Connection: close',
           '',
           payload
           ])
      _send_tcp((self.ip, self.port), header)

   def pause(self):
      pass

   def stop(self):
      pass

   def set_next(self, url):
      pass

   def next(self):
      pass

def discover(name = '', timeout = 1, st = "ssdp:all", mx = 3, debug = False):
   """ Discover UPnP devices in the local network.

   name -- name or part of the name to filter devices
   timeout -- timeout to perform discover
   st -- st field of discovery packet
   mx -- mx field of discovery packet
   debug -- True if debug output is required
   return -- list of DlnapDevice
   """
   payload = "\r\n".join([
              'M-SEARCH * HTTP/1.1',
              'HOST: {}:{}'.format(*SSDP_GROUP),
              'Accept: */*',
              'MAN: "ssdp:discover"',
              'ST: {}'.format(st),
              'MX: {}'.format(mx),
              '',
              ''])
   devices = []
   with _send_udp(SSDP_GROUP, payload) as sock:
      start = time.time()
      while True:
         if time.time() - start > timeout:
            # timedout
            break
         r, w, x = select.select([sock], [], [sock], 1)
         if sock in r:
             data, addr = sock.recvfrom(1024)
             d = DlnapDevice(data, addr[0], debug=debug)
             if d not in devices:
                if not name or name is None or name.lower() in d.name.lower():
                   devices.append(d)
         elif sock in x:
             raise Exception('Getting response failed')
         else:
             # Nothing to read
             pass
   return devices

if __name__ == '__main__':
   import getopt

   def usage():
      print('dlna.py [--list] [-d[evice] <name>] [-t[imeout] <seconds>] [--play <url>]')

   try:
      opts, args = getopt.getopt(sys.argv[1:], "hvd:t:", ['help', 'version', 'debug', 'play=', 'pause', 'stop', 'list', 'device=', 'timeout='])
   except getopt.GetoptError:
      usage()
      sys.exit(1)

   device = ''
   url = ''
   timeout = 0.5
   action = ''
   debug = False
   for opt, arg in opts:
      if opt in ('-h', '--help'):
         usage()
         sys.exit(0)
      if opt in ('-v', '--version'):
         print(__version__)
         sys.exit(0)
      if opt in ('--debug'):
         debug = True
      elif opt in ('-d', '--device'):
         device = arg
      elif opt in ('-t', '--timeout'):
         timeout = float(arg)
      if opt in ('--list'):
         action = 'list'
      elif opt in ('--play'):
         action = 'play'
         url = arg
      elif opt in ('--pause'):
         action = 'pause'
      elif opt in ('--stop'):
         action = 'stop'

   allDevices = discover(name=device, timeout=timeout, debug=debug)
   if not allDevices:
      print('No devices found.')
      sys.exit(1)

   if action in ('', 'list'):
      print('Discovered devices:')
      for d in allDevices:
         print(' {} {}'.format('+' if d.has_av_transport else '-', d))
      sys.exit(0)

   d = allDevices[0]
   print(d)
   if action == 'play':
      try:
         d.set_current(url=url)
         d.play()
      except Exception as e:
         print('Device is unable to play media.')
         if debug:
            print('Location: {}:{}'.format(d.location, d.port))
            print(e)
         sys.exit(1)
   elif action == 'pause':
      d.pause()
   elif action == 'stop':
      d.stop()
<|endoftext|>


<commit_before><commit_msg>use clearer method for making waveGrid<commit_after><|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""Tests for completerlib.

"""

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import os
import shutil
import sys
import tempfile
import unittest
from os.path import join

import nose.tools as nt

from IPython.core.completerlib import magic_run_completer, module_completion, try_import
from IPython.utils.tempdir import TemporaryDirectory
from IPython.testing.decorators import onlyif_unicode_paths

class MockEvent(object):
    def __init__(self, line):
        self.line = line

#-----------------------------------------------------------------------------
# Test functions begin
#-----------------------------------------------------------------------------
class Test_magic_run_completer(unittest.TestCase):
    files = [u"aao.py", u"a.py", u"b.py", u"aao.txt"]
    dirs = [u"adir/", "bdir/"]

    def setUp(self):
        self.BASETESTDIR = tempfile.mkdtemp()
        for fil in self.files:
            with open(join(self.BASETESTDIR, fil), "w") as sfile:
                sfile.write("pass\n")
        for d in self.dirs:
            os.mkdir(join(self.BASETESTDIR, d))

        self.oldpath = os.getcwd()
        os.chdir(self.BASETESTDIR)

    def tearDown(self):
        os.chdir(self.oldpath)
        shutil.rmtree(self.BASETESTDIR)

    def test_1(self):
        """Test magic_run_completer, should match two alterntives
        """
        event = MockEvent(u"%run a")
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})

    def test_2(self):
        """Test magic_run_completer, should match one alterntive
        """
        event = MockEvent(u"%run aa")
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"aao.py"})

    def test_3(self):
        """Test magic_run_completer with unterminated " """
        event = MockEvent(u'%run "a')
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})

    def test_completion_more_args(self):
        event = MockEvent(u'%run a.py ')
        match = set(magic_run_completer(None, event))
        self.assertEqual(match, set(self.files + self.dirs))

    def test_completion_in_dir(self):
        # Github issue #3459
        event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a')))
        print(repr(event.line))
        match = set(magic_run_completer(None, event))
        # We specifically use replace here rather than normpath, because
        # at one point there were duplicates 'adir' and 'adir/', and normpath
        # would hide the failure for that.
        self.assertEqual(match, {join(self.BASETESTDIR, f).replace('\\','/')
                            for f in (u'a.py', u'aao.py', u'aao.txt', u'adir/')})

class Test_magic_run_completer_nonascii(unittest.TestCase):
    @onlyif_unicode_paths
    def setUp(self):
        self.BASETESTDIR = tempfile.mkdtemp()
        for fil in [u"aaø.py", u"a.py", u"b.py"]:
            with open(join(self.BASETESTDIR, fil), "w") as sfile:
                sfile.write("pass\n")
        self.oldpath = os.getcwd()
        os.chdir(self.BASETESTDIR)

    def tearDown(self):
        os.chdir(self.oldpath)
        shutil.rmtree(self.BASETESTDIR)

    @onlyif_unicode_paths
    def test_1(self):
        """Test magic_run_completer, should match two alterntives
        """
        event = MockEvent(u"%run a")
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"a.py", u"aaø.py"})

    @onlyif_unicode_paths
    def test_2(self):
        """Test magic_run_completer, should match one alterntive
        """
        event = MockEvent(u"%run aa")
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"aaø.py"})

    @onlyif_unicode_paths
    def test_3(self):
        """Test magic_run_completer with unterminated " """
        event = MockEvent(u'%run "a')
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"a.py", u"aaø.py"})

# module_completer:

def test_import_invalid_module():
    """Testing of issue https://github.com/ipython/ipython/issues/1107"""
    invalid_module_names = {'foo-bar', 'foo:bar', '10foo'}
    valid_module_names = {'foobar'}
    with TemporaryDirectory() as tmpdir:
        sys.path.insert( 0, tmpdir )
        for name in invalid_module_names | valid_module_names:
            filename = os.path.join(tmpdir, name + '.py')
            open(filename, 'w').close()

        s = set( module_completion('import foo') )
        intersection = s.intersection(invalid_module_names)
        nt.assert_equal(intersection, set())

        assert valid_module_names.issubset(s), valid_module_names.intersection(s)

def test_bad_module_all():
    """Test module with invalid __all__

    https://github.com/ipython/ipython/issues/9678
    """
    testsdir = os.path.dirname(__file__)
    sys.path.insert(0, testsdir)
    try:
        results = module_completion('from bad_all import ')
        nt.assert_in('puppies', results)
        for r in results:
            nt.assert_is_instance(r, str)
    finally:
        sys.path.remove(testsdir)

def test_module_without_init():
    """
    Test module without __init__.py.

    https://github.com/ipython/ipython/issues/11226
    """
    fake_module_name = "foo"
    with TemporaryDirectory() as tmpdir:
        sys.path.insert(0, tmpdir)
        os.makedirs(os.path.join(tmpdir, fake_module_name))
        s = try_import(mod=fake_module_name)
        assert s == []
<commit_msg>Reset sys.path after test<commit_after># -*- coding: utf-8 -*-
"""Tests for completerlib.

"""

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import os
import shutil
import sys
import tempfile
import unittest
from os.path import join

import nose.tools as nt

from IPython.core.completerlib import magic_run_completer, module_completion, try_import
from IPython.utils.tempdir import TemporaryDirectory
from IPython.testing.decorators import onlyif_unicode_paths

class MockEvent(object):
    def __init__(self, line):
        self.line = line

#-----------------------------------------------------------------------------
# Test functions begin
#-----------------------------------------------------------------------------
class Test_magic_run_completer(unittest.TestCase):
    files = [u"aao.py", u"a.py", u"b.py", u"aao.txt"]
    dirs = [u"adir/", "bdir/"]

    def setUp(self):
        self.BASETESTDIR = tempfile.mkdtemp()
        for fil in self.files:
            with open(join(self.BASETESTDIR, fil), "w") as sfile:
                sfile.write("pass\n")
        for d in self.dirs:
            os.mkdir(join(self.BASETESTDIR, d))

        self.oldpath = os.getcwd()
        os.chdir(self.BASETESTDIR)

    def tearDown(self):
        os.chdir(self.oldpath)
        shutil.rmtree(self.BASETESTDIR)

    def test_1(self):
        """Test magic_run_completer, should match two alterntives
        """
        event = MockEvent(u"%run a")
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})

    def test_2(self):
        """Test magic_run_completer, should match one alterntive
        """
        event = MockEvent(u"%run aa")
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"aao.py"})

    def test_3(self):
        """Test magic_run_completer with unterminated " """
        event = MockEvent(u'%run "a')
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})

    def test_completion_more_args(self):
        event = MockEvent(u'%run a.py ')
        match = set(magic_run_completer(None, event))
        self.assertEqual(match, set(self.files + self.dirs))

    def test_completion_in_dir(self):
        # Github issue #3459
        event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a')))
        print(repr(event.line))
        match = set(magic_run_completer(None, event))
        # We specifically use replace here rather than normpath, because
        # at one point there were duplicates 'adir' and 'adir/', and normpath
        # would hide the failure for that.
        self.assertEqual(match, {join(self.BASETESTDIR, f).replace('\\','/')
                            for f in (u'a.py', u'aao.py', u'aao.txt', u'adir/')})

class Test_magic_run_completer_nonascii(unittest.TestCase):
    @onlyif_unicode_paths
    def setUp(self):
        self.BASETESTDIR = tempfile.mkdtemp()
        for fil in [u"aaø.py", u"a.py", u"b.py"]:
            with open(join(self.BASETESTDIR, fil), "w") as sfile:
                sfile.write("pass\n")
        self.oldpath = os.getcwd()
        os.chdir(self.BASETESTDIR)

    def tearDown(self):
        os.chdir(self.oldpath)
        shutil.rmtree(self.BASETESTDIR)

    @onlyif_unicode_paths
    def test_1(self):
        """Test magic_run_completer, should match two alterntives
        """
        event = MockEvent(u"%run a")
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"a.py", u"aaø.py"})

    @onlyif_unicode_paths
    def test_2(self):
        """Test magic_run_completer, should match one alterntive
        """
        event = MockEvent(u"%run aa")
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"aaø.py"})

    @onlyif_unicode_paths
    def test_3(self):
        """Test magic_run_completer with unterminated " """
        event = MockEvent(u'%run "a')
        mockself = None
        match = set(magic_run_completer(mockself, event))
        self.assertEqual(match, {u"a.py", u"aaø.py"})

# module_completer:

def test_import_invalid_module():
    """Testing of issue https://github.com/ipython/ipython/issues/1107"""
    invalid_module_names = {'foo-bar', 'foo:bar', '10foo'}
    valid_module_names = {'foobar'}
    with TemporaryDirectory() as tmpdir:
        sys.path.insert( 0, tmpdir )
        for name in invalid_module_names | valid_module_names:
            filename = os.path.join(tmpdir, name + '.py')
            open(filename, 'w').close()

        s = set( module_completion('import foo') )
        intersection = s.intersection(invalid_module_names)
        nt.assert_equal(intersection, set())

        assert valid_module_names.issubset(s), valid_module_names.intersection(s)

def test_bad_module_all():
    """Test module with invalid __all__

    https://github.com/ipython/ipython/issues/9678
    """
    testsdir = os.path.dirname(__file__)
    sys.path.insert(0, testsdir)
    try:
        results = module_completion('from bad_all import ')
        nt.assert_in('puppies', results)
        for r in results:
            nt.assert_is_instance(r, str)
    finally:
        sys.path.remove(testsdir)

def test_module_without_init():
    """
    Test module without __init__.py.

    https://github.com/ipython/ipython/issues/11226
    """
    fake_module_name = "foo"
    with TemporaryDirectory() as tmpdir:
        sys.path.insert(0, tmpdir)
        try:
            os.makedirs(os.path.join(tmpdir, fake_module_name))
            s = try_import(mod=fake_module_name)
            assert s == []
        finally:
            sys.path.remove(tmpdir)
<|endoftext|>


<commit_before># -*- coding: UTF8 -*-
import datetime
import connections

def longNumber(x):
    '''
    Convert number coming from the JSON-RPC to a human readable format with 8 decimal
    '''
    return "{:.8f}".format(x)

def twitterizeDate(ts):
    '''
    Make a timestamp prettier
    '''
    mydate = datetime.datetime.fromtimestamp(ts)
    difference = datetime.datetime.now() - mydate
    s = difference.seconds
    if difference.days > 7 or difference.days < 0:
        return mydate.strftime('%d %b %y')
    elif difference.days == 1:
        return '1 day ago'
    elif difference.days > 1:
        return '{} days ago'.format(difference.days)
    elif s <= 1:
        return 'just now'
    elif s < 60:
        return '{} seconds ago'.format(s)
    elif s < 120:
        return '1 minute ago'
    elif s < 3600:
        return '{} minutes ago'.format(s/60)
    elif s < 7200:
        return '1 hour ago'
    else:
        return '{} hours ago'.format(s/3600)

def getAllAccounts(connector):
    '''
    Return all accounts
    '''
    accounts = []
    accounts_by_name = connector.listaccounts()
    for currency in accounts_by_name.keys():
        for account in accounts_by_name[currency]:
            address = connector.getaddressesbyaccount(account['name'], currency)
            account['address'] = address
            account['currency'] = currency
            account['currency_symbol'] = getCurrencySymbol(currency)
            accounts.append(account)

    return accounts

def getAccountsWithNames(connector):
    '''
    Return accounts that have names only
    '''
    a = getAllAccounts(connector)
    accounts_with_names = []
    for acc in a:
        if acc['name']:
            accounts_with_names.append(acc);

    return accounts_with_names

def getTransactions(connector, account_name = None, sort_by = 'timereceived', reverse_order = False):
    '''
    Return transactions by account name
    '''
    transactions_ordered = []
    transactions = connector.listtransactions(account_name)
    for currency in transactions.keys():
        for transaction in transactions[currency]:
            transaction['currency'] = currency.upper()
            transaction['timereceived_pretty'] = twitterizeDate(transaction.get('timereceived', 0))
            transaction['time_pretty'] = twitterizeDate(transaction.get('time', 0))
            transaction['timereceived_human'] = datetime.datetime.fromtimestamp(transaction.get('timereceived', 0))
            transaction['time_human'] = datetime.datetime.fromtimestamp(transaction.get('time', 0))
            transactions_ordered.append(transaction)

    transactions_ordered = sorted(transactions_ordered, key=lambda k: k[sort_by], reverse=reverse_order)
    return transactions_ordered

def getSiteSections(active):
    sections = [
                    {'name': 'dashboard', 'path':"/dashboard", 'title': 'Dashboard'},
                    {'name': 'accounts', 'path':"/accounts", 'title': 'Accounts'},
                    {'name': 'transactions', 'path':"/transactions/1", 'title': "Transactions"},
                    {'name': 'transfer', 'path':"/transfer", 'title': "Transfer"},
                    {'name': 'addressbook', 'path':"/addressbook", 'title': "Addressbook"},
               ]

    for section in sections:
        if section['name'] == active:
            section['active'] = True
    return sections

def getCurrencySymbol(for_currency='*'):
    currencies = {}
    connection_config = connections.connector.config
    for currency in connection_config.keys():
        currencies[currency] = connections.connector.config[currency]['symbol']

    if for_currency == '*':
        return currencies
    else:
        return currencies[for_currency]

<commit_msg>proper getting<commit_after># -*- coding: UTF8 -*-
import datetime
import connections

def longNumber(x):
    '''
    Convert number coming from the JSON-RPC to a human readable format with 8 decimal
    '''
    return "{:.8f}".format(x)

def twitterizeDate(ts):
    '''
    Make a timestamp prettier
    '''
    mydate = datetime.datetime.fromtimestamp(ts)
    difference = datetime.datetime.now() - mydate
    s = difference.seconds
    if difference.days > 7 or difference.days < 0:
        return mydate.strftime('%d %b %y')
    elif difference.days == 1:
        return '1 day ago'
    elif difference.days > 1:
        return '{} days ago'.format(difference.days)
    elif s <= 1:
        return 'just now'
    elif s < 60:
        return '{} seconds ago'.format(s)
    elif s < 120:
        return '1 minute ago'
    elif s < 3600:
        return '{} minutes ago'.format(s/60)
    elif s < 7200:
        return '1 hour ago'
    else:
        return '{} hours ago'.format(s/3600)

def getAllAccounts(connector):
    '''
    Return all accounts
    '''
    accounts = []
    accounts_by_name = connector.listaccounts()
    for currency in accounts_by_name.keys():
        for account in accounts_by_name[currency]:
            address = connector.getaddressesbyaccount(account['name'], currency)
            account['address'] = address
            account['currency'] = currency
            account['currency_symbol'] = getCurrencySymbol(currency)
            accounts.append(account)

    return accounts

def getAccountsWithNames(connector):
    '''
    Return accounts that have names only
    '''
    a = getAllAccounts(connector)
    accounts_with_names = []
    for acc in a:
        if acc['name']:
            accounts_with_names.append(acc);

    return accounts_with_names

def getTransactions(connector, account_name = None, sort_by = 'timereceived', reverse_order = False):
    '''
    Return transactions by account name
    '''
    transactions_ordered = []
    transactions = connector.listtransactions(account_name)
    for currency in transactions.keys():
        for transaction in transactions[currency]:
            transaction['currency'] = currency.upper()
            transaction['timereceived_pretty'] = twitterizeDate(transaction.get('timereceived', 0))
            transaction['time_pretty'] = twitterizeDate(transaction.get('time', 0))
            transaction['timereceived_human'] = datetime.datetime.fromtimestamp(transaction.get('timereceived', 0))
            transaction['time_human'] = datetime.datetime.fromtimestamp(transaction.get('time', 0))
            transactions_ordered.append(transaction)

    transactions_ordered = sorted(transactions_ordered, key=lambda k: k.get(sort_by,0), reverse=reverse_order)
    return transactions_ordered

def getSiteSections(active):
    sections = [
                    {'name': 'dashboard', 'path':"/dashboard", 'title': 'Dashboard'},
                    {'name': 'accounts', 'path':"/accounts", 'title': 'Accounts'},
                    {'name': 'transactions', 'path':"/transactions/1", 'title': "Transactions"},
                    {'name': 'transfer', 'path':"/transfer", 'title': "Transfer"},
                    {'name': 'addressbook', 'path':"/addressbook", 'title': "Addressbook"},
               ]

    for section in sections:
        if section['name'] == active:
            section['active'] = True
    return sections

def getCurrencySymbol(for_currency='*'):
    currencies = {}
    connection_config = connections.connector.config
    for currency in connection_config.keys():
        currencies[currency] = connections.connector.config[currency]['symbol']

    if for_currency == '*':
        return currencies
    else:
        return currencies[for_currency]

<|endoftext|>


<commit_before>import os
from unittest import TestCase

from IPython.testing.decorators import skip
from IPython.utils.tempdir import TemporaryDirectory
from IPython.utils.pickleshare import PickleShareDB

class PickleShareDBTestCase(TestCase):
    def setUp(self):
        self.tempdir = TemporaryDirectory()

    def tearDown(self):
        self.tempdir.cleanup()

    def test_picklesharedb(self):
        db = PickleShareDB(self.tempdir.name)
        db.clear()
        print("Should be empty:",db.items())
        db['hello'] = 15
        db['aku ankka'] = [1,2,313]
        db['paths/nest/ok/keyname'] = [1,(5,46)]
        db.hset('hash', 'aku', 12)
        db.hset('hash', 'ankka', 313)
        self.assertEqual(db.hget('hash','aku'), 12)
        self.assertEqual(db.hget('hash','ankka'), 313)
        print("all hashed",db.hdict('hash'))
        print(db.keys())
        print(db.keys('paths/nest/ok/k*'))
        print(dict(db)) # snapsot of whole db
        db.uncache() # frees memory, causes re-reads later

        # shorthand for accessing deeply nested files
        lnk = db.getlink('myobjects/test')
        lnk.foo = 2
        lnk.bar = lnk.foo + 5
        self.assertEqual(lnk.bar, 7)

    @skip("Too slow for regular running.")
    def test_stress(self):
        db = PickleShareDB('~/fsdbtest')
        import time,sys
        for i in range(1000):
            for j in range(1000):
                if i % 15 == 0 and i < 200:
                    if str(j) in db:
                        del db[str(j)]
                    continue

                if j%33 == 0:
                    time.sleep(0.02)

                db[str(j)] = db.get(str(j), []) + [(i,j,"proc %d" % os.getpid())]
                db.hset('hash',j, db.hget('hash',j,15) + 1 )

            print(i, end=' ')
            sys.stdout.flush()
            if i % 10 == 0:
                db.uncache()<commit_msg>Use print_function<commit_after>from __future__ import print_function

import os
from unittest import TestCase

from IPython.testing.decorators import skip
from IPython.utils.tempdir import TemporaryDirectory
from IPython.utils.pickleshare import PickleShareDB

class PickleShareDBTestCase(TestCase):
    def setUp(self):
        self.tempdir = TemporaryDirectory()

    def tearDown(self):
        self.tempdir.cleanup()

    def test_picklesharedb(self):
        db = PickleShareDB(self.tempdir.name)
        db.clear()
        print("Should be empty:",db.items())
        db['hello'] = 15
        db['aku ankka'] = [1,2,313]
        db['paths/nest/ok/keyname'] = [1,(5,46)]
        db.hset('hash', 'aku', 12)
        db.hset('hash', 'ankka', 313)
        self.assertEqual(db.hget('hash','aku'), 12)
        self.assertEqual(db.hget('hash','ankka'), 313)
        print("all hashed",db.hdict('hash'))
        print(db.keys())
        print(db.keys('paths/nest/ok/k*'))
        print(dict(db)) # snapsot of whole db
        db.uncache() # frees memory, causes re-reads later

        # shorthand for accessing deeply nested files
        lnk = db.getlink('myobjects/test')
        lnk.foo = 2
        lnk.bar = lnk.foo + 5
        self.assertEqual(lnk.bar, 7)

    @skip("Too slow for regular running.")
    def test_stress(self):
        db = PickleShareDB('~/fsdbtest')
        import time,sys
        for i in range(1000):
            for j in range(1000):
                if i % 15 == 0 and i < 200:
                    if str(j) in db:
                        del db[str(j)]
                    continue

                if j%33 == 0:
                    time.sleep(0.02)

                db[str(j)] = db.get(str(j), []) + [(i,j,"proc %d" % os.getpid())]
                db.hset('hash',j, db.hget('hash',j,15) + 1 )

            print(i, end=' ')
            sys.stdout.flush()
            if i % 10 == 0:
                db.uncache()<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# name:             interactive_test.py
# author:           Harold Bradley III
# email:            harold@bradleystudio.net
# created on:       02/26/2016
#

"""
ext_pylib.terminal.interactive_test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This module is just an interactive test for the ansi and colors modules.
"""

from __future__ import absolute_import, print_function

from timeit import timeit

from ext_pylib.terminal import ansi, colors
from ext_pylib.input import prompt

def print_all_colors():  # pylint: disable=too-many-statements
    """Prints all text effects, basic colors, and background colors with a
    string of the function name."""
    print('\n\t----------------------------')
    print('\tPrinting All Color Functions')
    print('\t----------------------------')

    print('\tnormal: \t\t' + colors.normal('normal'))
    print('\tunderline: \t\t' + colors.underline('underline'))
    print('\tbold: \t\t\t' + colors.bold('bold'))
    print('\tblink: \t\t\t' + colors.blink('blink'))
    print('\trblink: \t\t' + colors.rblink('rblink'))
    print('\treverse: \t\t' + colors.reverse('reverse'))
    print('\tconceal: \t\t' + colors.conceal('conceal'))
    print('\tblack: \t\t' + colors.black('black'))
    print('\tred: \t\t\t' + colors.red('red'))
    print('\tgreen: \t\t\t' + colors.green('green'))
    print('\tyellow: \t\t' + colors.yellow('yellow'))
    print('\tblue: \t\t\t' + colors.blue('blue'))
    print('\tmagenta: \t\t' + colors.magenta('magenta'))
    print('\tcyan: \t\t\t' + colors.cyan('cyan'))
    print('\twhite: \t\t\t' + colors.white('white'))
    print('\ton_black: \t\t' + colors.on_black('on_black'))
    print('\ton_red: \t\t' + colors.on_red('on_red'))
    print('\ton_green: \t\t' + colors.on_green('on_green'))
    print('\ton_yellow: \t\t' + colors.on_yellow('on_yellow'))
    print('\ton_blue: \t\t' + colors.on_blue('on_blue'))
    print('\ton_magenta: \t\t' + colors.on_magenta('on_magenta'))
    print('\ton_cyan: \t\t' + colors.on_cyan('on_cyan'))
    print('\ton_white: \t\t' + colors.on_white('on_white'))
    print('\tred_on_black: \t\t' + colors.red_on_black('red_on_black'))
    print('\tgreen_on_black: \t' + colors.green_on_black('green_on_black'))
    print('\tyellow_on_black: \t' + colors.yellow_on_black('yellow_on_black'))
    print('\tblue_on_black: \t\t' + colors.blue_on_black('blue_on_black'))
    print('\tmagenta_on_black: \t' + colors.magenta_on_black('magenta_on_black'))
    print('\tcyan_on_black: \t\t' + colors.cyan_on_black('cyan_on_black'))
    print('\twhite_on_black: \t' + colors.white_on_black('white_on_black'))
    print('\tblack_on_red: \t\t' + colors.black_on_red('black_on_red'))
    print('\tgreen_on_red: \t\t' + colors.green_on_red('green_on_red'))
    print('\tyellow_on_red: \t\t' + colors.yellow_on_red('yellow_on_red'))
    print('\tblue_on_red: \t\t' + colors.blue_on_red('blue_on_red'))
    print('\tmagenta_on_red: \t' + colors.magenta_on_red('magenta_on_red'))
    print('\tcyan_on_red: \t\t' + colors.cyan_on_red('cyan_on_red'))
    print('\twhite_on_red: \t\t' + colors.white_on_red('white_on_red'))
    print('\tblack_on_green: \t' + colors.black_on_green('black_on_green'))
    print('\tred_on_green: \t\t' + colors.red_on_green('red_on_green'))
    print('\tyellow_on_green: \t' + colors.yellow_on_green('yellow_on_green'))
    print('\tblue_on_green: \t\t' + colors.blue_on_green('blue_on_green'))
    print('\tmagenta_on_green: \t' + colors.magenta_on_green('magenta_on_green'))
    print('\tcyan_on_green: \t\t' + colors.cyan_on_green('cyan_on_green'))
    print('\twhite_on_green: \t' + colors.white_on_green('white_on_green'))
    print('\tblack_on_yellow: \t' + colors.black_on_yellow('black_on_yellow'))
    print('\tred_on_yellow: \t\t' + colors.red_on_yellow('red_on_yellow'))
    print('\tgreen_on_yellow: \t' + colors.green_on_yellow('green_on_yellow'))
    print('\tblue_on_yellow: \t' + colors.blue_on_yellow('blue_on_yellow'))
    print('\tmagenta_on_yellow: \t' + colors.magenta_on_yellow('magenta_on_yellow'))
    print('\tcyan_on_yellow: \t' + colors.cyan_on_yellow('cyan_on_yellow'))
    print('\twhite_on_yellow: \t' + colors.white_on_yellow('white_on_yellow'))
    print('\tblack_on_blue: \t\t' + colors.black_on_blue('black_on_blue'))
    print('\tred_on_blue: \t\t' + colors.red_on_blue('red_on_blue'))
    print('\tgreen_on_blue: \t\t' + colors.green_on_blue('green_on_blue'))
    print('\tyellow_on_blue: \t' + colors.yellow_on_blue('yellow_on_blue'))
    print('\tmagenta_on_blue: \t' + colors.magenta_on_blue('magenta_on_blue'))
    print('\tcyan_on_blue: \t\t' + colors.cyan_on_blue('cyan_on_blue'))
    print('\twhite_on_blue: \t\t' + colors.white_on_blue('white_on_blue'))
    print('\tblack_on_magenta: \t' + colors.black_on_magenta('black_on_magenta'))
    print('\tred_on_magenta: \t' + colors.red_on_magenta('red_on_magenta'))
    print('\tgreen_on_magenta: \t' + colors.green_on_magenta('green_on_magenta'))
    print('\tyellow_on_magenta: \t' + colors.yellow_on_magenta('yellow_on_magenta'))
    print('\tblue_on_magenta: \t' + colors.blue_on_magenta('blue_on_magenta'))
    print('\tcyan_on_magenta: \t' + colors.cyan_on_magenta('cyan_on_magenta'))
    print('\twhite_on_magenta: \t' + colors.white_on_magenta('white_on_magenta'))
    print('\tblack_on_cyan: \t\t' + colors.black_on_cyan('black_on_cyan'))
    print('\tred_on_cyan: \t\t' + colors.red_on_cyan('red_on_cyan'))
    print('\tgreen_on_cyan: \t\t' + colors.green_on_cyan('green_on_cyan'))
    print('\tyellow_on_cyan: \t' + colors.yellow_on_cyan('yellow_on_cyan'))
    print('\tblue_on_cyan: \t\t' + colors.blue_on_cyan('blue_on_cyan'))
    print('\tmagenta_on_cyan: \t' + colors.magenta_on_cyan('magenta_on_cyan'))
    print('\twhite_on_cyan: \t\t' + colors.white_on_cyan('white_on_cyan'))
    print('\tblack_on_white: \t' + colors.black_on_white('black_on_white'))
    print('\tred_on_white: \t\t' + colors.red_on_white('red_on_white'))
    print('\tgreen_on_white: \t' + colors.green_on_white('green_on_white'))
    print('\tyellow_on_white: \t' + colors.yellow_on_white('yellow_on_white'))
    print('\tblue_on_white: \t\t' + colors.blue_on_white('blue_on_white'))
    print('\tmagenta_on_white: \t' + colors.magenta_on_white('magenta_on_white'))
    print('\tcyan_on_white: \t\t' + colors.cyan_on_white('cyan_on_white'))

def main():
    """The main test suite."""
    ansi.reset()

    print(timeit(print_all_colors, number=1))

    if not prompt("Continue?"):
        return False

    ansi.reset()

    print('ext_pylib interactive terminal test')
    ansi.cursor_down(2)

    print('Testing colors...')
    print('Row 1:  | Row 2:   | ')
    print(colors.black_on_red('black'))
    print(colors.red('red'))
    print(colors.green('green'))
    print(colors.yellow('yellow'))
    print(colors.blue('blue'))
    print(colors.magenta('magenta'))
    print(colors.cyan('cyan'))
    print(colors.white('white'))

    ansi.cursor_up(8)
    ansi.cursor_right(10)

    print(colors.black_on_white('black'))
    ansi.cursor_right(10)
    print(colors.red_on_black('red'))
    ansi.cursor_right(10)
    print(colors.green_on_red('green'))
    ansi.cursor_right(10)
    print(colors.yellow_on_green('yellow'))
    ansi.cursor_right(10)
    print(colors.blue_on_yellow('blue'))
    ansi.cursor_right(10)
    print(colors.magenta_on_blue('magenta'))
    ansi.cursor_right(10)
    print(colors.cyan_on_magenta('cyan'))
    ansi.cursor_right(10)
    print(colors.white_on_cyan('white'))

    print('\n')
    print(colors.underline(colors.red('This is red text that is underlined')))
    print(colors.bold(colors.green('This is green text that is bold')))
    print(colors.reverse(colors.green_on_blue('This is green on blue text that is reversed')))

if __name__ == '__main__':
    main()
<commit_msg>Updates interactive tests for terminal module.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# name:             interactive_test.py
# author:           Harold Bradley III
# email:            harold@bradleystudio.net
# created on:       02/26/2016
#

"""
ext_pylib.terminal.interactive_test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This module is just an interactive test for the ansi and colors modules.
"""

from __future__ import absolute_import, print_function

from timeit import timeit

from ext_pylib.terminal import ansi, colors
from ext_pylib.input import prompt

def print_all_colors():  # pylint: disable=too-many-statements
    """Prints all text effects, basic colors, and background colors with a
    string of the function name."""
    print('\n\t----------------------------')
    print('\tPrinting All Color Functions')
    print('\t----------------------------')

    print('\tnormal: \t\t' + colors.normal('normal'))
    print('\tunderline: \t\t' + colors.underline('underline'))
    print('\tbold: \t\t\t' + colors.bold('bold'))
    print('\tblink: \t\t\t' + colors.blink('blink'))
    print('\trblink: \t\t' + colors.rblink('rblink'))
    print('\treverse: \t\t' + colors.reverse('reverse'))
    print('\tconceal: \t\t' + colors.conceal('conceal'))
    print('\tblack: \t\t' + colors.black('black'))
    print('\tred: \t\t\t' + colors.red('red'))
    print('\tgreen: \t\t\t' + colors.green('green'))
    print('\tyellow: \t\t' + colors.yellow('yellow'))
    print('\tblue: \t\t\t' + colors.blue('blue'))
    print('\tmagenta: \t\t' + colors.magenta('magenta'))
    print('\tcyan: \t\t\t' + colors.cyan('cyan'))
    print('\twhite: \t\t\t' + colors.white('white'))
    print('\ton_black: \t\t' + colors.on_black('on_black'))
    print('\ton_red: \t\t' + colors.on_red('on_red'))
    print('\ton_green: \t\t' + colors.on_green('on_green'))
    print('\ton_yellow: \t\t' + colors.on_yellow('on_yellow'))
    print('\ton_blue: \t\t' + colors.on_blue('on_blue'))
    print('\ton_magenta: \t\t' + colors.on_magenta('on_magenta'))
    print('\ton_cyan: \t\t' + colors.on_cyan('on_cyan'))
    print('\ton_white: \t\t' + colors.on_white('on_white'))
    print('\tred_on_black: \t\t' + colors.red_on_black('red_on_black'))
    print('\tgreen_on_black: \t' + colors.green_on_black('green_on_black'))
    print('\tyellow_on_black: \t' + colors.yellow_on_black('yellow_on_black'))
    print('\tblue_on_black: \t\t' + colors.blue_on_black('blue_on_black'))
    print('\tmagenta_on_black: \t' + colors.magenta_on_black('magenta_on_black'))
    print('\tcyan_on_black: \t\t' + colors.cyan_on_black('cyan_on_black'))
    print('\twhite_on_black: \t' + colors.white_on_black('white_on_black'))
    print('\tblack_on_red: \t\t' + colors.black_on_red('black_on_red'))
    print('\tgreen_on_red: \t\t' + colors.green_on_red('green_on_red'))
    print('\tyellow_on_red: \t\t' + colors.yellow_on_red('yellow_on_red'))
    print('\tblue_on_red: \t\t' + colors.blue_on_red('blue_on_red'))
    print('\tmagenta_on_red: \t' + colors.magenta_on_red('magenta_on_red'))
    print('\tcyan_on_red: \t\t' + colors.cyan_on_red('cyan_on_red'))
    print('\twhite_on_red: \t\t' + colors.white_on_red('white_on_red'))
    print('\tblack_on_green: \t' + colors.black_on_green('black_on_green'))
    print('\tred_on_green: \t\t' + colors.red_on_green('red_on_green'))
    print('\tyellow_on_green: \t' + colors.yellow_on_green('yellow_on_green'))
    print('\tblue_on_green: \t\t' + colors.blue_on_green('blue_on_green'))
    print('\tmagenta_on_green: \t' + colors.magenta_on_green('magenta_on_green'))
    print('\tcyan_on_green: \t\t' + colors.cyan_on_green('cyan_on_green'))
    print('\twhite_on_green: \t' + colors.white_on_green('white_on_green'))
    print('\tblack_on_yellow: \t' + colors.black_on_yellow('black_on_yellow'))
    print('\tred_on_yellow: \t\t' + colors.red_on_yellow('red_on_yellow'))
    print('\tgreen_on_yellow: \t' + colors.green_on_yellow('green_on_yellow'))
    print('\tblue_on_yellow: \t' + colors.blue_on_yellow('blue_on_yellow'))
    print('\tmagenta_on_yellow: \t' + colors.magenta_on_yellow('magenta_on_yellow'))
    print('\tcyan_on_yellow: \t' + colors.cyan_on_yellow('cyan_on_yellow'))
    print('\twhite_on_yellow: \t' + colors.white_on_yellow('white_on_yellow'))
    print('\tblack_on_blue: \t\t' + colors.black_on_blue('black_on_blue'))
    print('\tred_on_blue: \t\t' + colors.red_on_blue('red_on_blue'))
    print('\tgreen_on_blue: \t\t' + colors.green_on_blue('green_on_blue'))
    print('\tyellow_on_blue: \t' + colors.yellow_on_blue('yellow_on_blue'))
    print('\tmagenta_on_blue: \t' + colors.magenta_on_blue('magenta_on_blue'))
    print('\tcyan_on_blue: \t\t' + colors.cyan_on_blue('cyan_on_blue'))
    print('\twhite_on_blue: \t\t' + colors.white_on_blue('white_on_blue'))
    print('\tblack_on_magenta: \t' + colors.black_on_magenta('black_on_magenta'))
    print('\tred_on_magenta: \t' + colors.red_on_magenta('red_on_magenta'))
    print('\tgreen_on_magenta: \t' + colors.green_on_magenta('green_on_magenta'))
    print('\tyellow_on_magenta: \t' + colors.yellow_on_magenta('yellow_on_magenta'))
    print('\tblue_on_magenta: \t' + colors.blue_on_magenta('blue_on_magenta'))
    print('\tcyan_on_magenta: \t' + colors.cyan_on_magenta('cyan_on_magenta'))
    print('\twhite_on_magenta: \t' + colors.white_on_magenta('white_on_magenta'))
    print('\tblack_on_cyan: \t\t' + colors.black_on_cyan('black_on_cyan'))
    print('\tred_on_cyan: \t\t' + colors.red_on_cyan('red_on_cyan'))
    print('\tgreen_on_cyan: \t\t' + colors.green_on_cyan('green_on_cyan'))
    print('\tyellow_on_cyan: \t' + colors.yellow_on_cyan('yellow_on_cyan'))
    print('\tblue_on_cyan: \t\t' + colors.blue_on_cyan('blue_on_cyan'))
    print('\tmagenta_on_cyan: \t' + colors.magenta_on_cyan('magenta_on_cyan'))
    print('\twhite_on_cyan: \t\t' + colors.white_on_cyan('white_on_cyan'))
    print('\tblack_on_white: \t' + colors.black_on_white('black_on_white'))
    print('\tred_on_white: \t\t' + colors.red_on_white('red_on_white'))
    print('\tgreen_on_white: \t' + colors.green_on_white('green_on_white'))
    print('\tyellow_on_white: \t' + colors.yellow_on_white('yellow_on_white'))
    print('\tblue_on_white: \t\t' + colors.blue_on_white('blue_on_white'))
    print('\tmagenta_on_white: \t' + colors.magenta_on_white('magenta_on_white'))
    print('\tcyan_on_white: \t\t' + colors.cyan_on_white('cyan_on_white'))

def print_assorted_combinations():
    """Prints an assorted combination of selected color functions."""
    print('ext_pylib interactive terminal test')
    ansi.cursor_down(2)

    print('Testing colors...')
    print('Row 1:  | Row 2:   | ')
    print(colors.black_on_red('black'))
    print(colors.red('red'))
    print(colors.green('green'))
    print(colors.yellow('yellow'))
    print(colors.blue('blue'))
    print(colors.magenta('magenta'))
    print(colors.cyan('cyan'))
    print(colors.white('white'))

    ansi.cursor_up(8)
    ansi.cursor_right(10)

    print(colors.black_on_white('black'))
    ansi.cursor_right(10)
    print(colors.red_on_black('red'))
    ansi.cursor_right(10)
    print(colors.green_on_red('green'))
    ansi.cursor_right(10)
    print(colors.yellow_on_green('yellow'))
    ansi.cursor_right(10)
    print(colors.blue_on_yellow('blue'))
    ansi.cursor_right(10)
    print(colors.magenta_on_blue('magenta'))
    ansi.cursor_right(10)
    print(colors.cyan_on_magenta('cyan'))
    ansi.cursor_right(10)
    print(colors.white_on_cyan('white'))

    print('\n')
    print(colors.underline(colors.red('This is red text that is underlined')))
    print(colors.bold(colors.green('This is green text that is bold')))
    print(colors.reverse(colors.green_on_blue('This is green on blue text that is reversed')))

def main():
    """The main test suite."""
    ansi.reset()

    print(timeit(print_all_colors, number=1))

    if not prompt("Continue?"):
        return False

    ansi.reset()

    print(timeit(print_assorted_combinations, number=1))

    if not prompt("Continue?"):
        return False

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


<commit_before># Copyright 2015 F5 Networks Inc.
#
# 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.
#

import mock
import pytest

from f5.bigip import ManagementRoot
from f5.bigip.resource import MissingRequiredCreationParameter
from f5.bigip.tm.ltm.data_group import Internal, External

@pytest.fixture
def FakeData_Group_Internal():
    fake_dg = mock.MagicMock()
    fake_dg = Internal(fake_dg)
    return fake_dg

@pytest.fixture
def FakeData_Group_External():
    fake_dg = mock.MagicMock()
    fake_dg = External(fake_dg)
    return fake_dg

class TestCreate(object):
    def test_create_internal_two(self, fakeicontrolsession):
        mgmt = ManagementRoot('172.16.44.15', 'admin', 'admin')
        dg1 = mgmt.tm.ltm.data_group.internals.internal
        dg2 = mgmt.tm.ltm.data_group.internals.internal
        assert dg1 is not dg2

    def test_create_external_two(self, fakeicontrolsession):
        mgmt = ManagementRoot('172.16.44.15', 'admin', 'admin')
        dg1 = mgmt.tm.ltm.data_group.externals.external
        dg2 = mgmt.tm.ltm.data_group.externals.external
        assert dg1 is not dg2

    def test_create_internal_no_args(self, FakeData_Group_Internal):
        with pytest.raises(MissingRequiredCreationParameter):
            FakeData_Group_Internal.create()

    def test_create_external_no_args(self, FakeData_Group_External):
        with pytest.raises(MissingRequiredCreationParameter):
            FakeData_Group_External.create()
<commit_msg>Fixes #315<commit_after># Copyright 2015 F5 Networks Inc.
#
# 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.
#

import mock
import pytest

from f5.bigip import ManagementRoot
from f5.bigip.resource import MissingRequiredCreationParameter
from f5.bigip.tm.ltm.data_group import Internal, External

@pytest.fixture
def FakeData_Group_Internal():
    fake_dg = mock.MagicMock()
    fake_dg = Internal(fake_dg)
    return fake_dg

@pytest.fixture
def FakeData_Group_External():
    fake_dg = mock.MagicMock()
    fake_dg = External(fake_dg)
    return fake_dg

class TestCreate(object):
    def test_create_internal_two(self, fakeicontrolsession):
        mgmt = ManagementRoot('172.16.44.15', 'admin', 'admin')
        dg1 = mgmt.tm.ltm.data_group.internals.internal
        dg2 = mgmt.tm.ltm.data_group.internals.internal
        assert dg1 is not dg2

    def test_create_external_two(self, fakeicontrolsession):
        mgmt = ManagementRoot('172.16.44.15', 'admin', 'admin')
        dg1 = mgmt.tm.ltm.data_group.externals.external
        dg2 = mgmt.tm.ltm.data_group.externals.external
        assert dg1 is not dg2

    def test_create_internal_no_args(self, FakeData_Group_Internal):
        with pytest.raises(MissingRequiredCreationParameter):
            FakeData_Group_Internal.create()

    def test_create_external_no_args(self, FakeData_Group_External):
        with pytest.raises(MissingRequiredCreationParameter):
            FakeData_Group_External.create()
<|endoftext|>


<commit_before>import time
import logging

import requests

from .leakybucket import LeakyBucket

class TokenExpireException(Exception):
    pass

# pylint: disable=too-many-instance-attributes,too-few-public-methods
class Torrent(object):
    '''
    brief
    {
        "filename":"Off.Piste.2016.iNTERNAL.BDRip.x264-LiBRARiANS",
        "category":"Movies/x264",
        "download":"magnet:..."
    }

    extened
    {
        "title":"Off.Piste.2016.iNTERNAL.BDRip.x264-LiBRARiANS",
        "category":"Movies/x264",
        "download":"magnet:...",
        "seeders":12,
        "leechers":6,
        "size":504519520,
        "pubdate":"2017-05-21 02:13:49 +0000",
        "episode_info":{
            "imdb":"tt4443856",
            "tvrage":null,
            "tvdb":null,
            "themoviedb":"430293"
        },
        "ranked":1,
        "info_page":"https://torrentapi.org/...."
    }
    '''
    def __init__(self, mapping):
        self._raw = mapping
        self.is_extended = 'title' in self._raw
        self.category = self._raw['category']
        self.download = self._raw['download']
        self.filename = self._raw.get('filename') or self._raw.get('title')
        self.size = self._raw.get('size')
        self.pubdate = self._raw.get('pubdate')
        self.page = self._raw.get('info_page')

    def __str__(self):
        return '%s(%s)' % (self.filename, self.category)

def json_hook(dct):
    error_code = dct.get('error_code')
    if error_code in [2, 4]:
        raise TokenExpireException('Token expired')
    if 'download' in dct:
        return Torrent(dct)
    return dct

class _RarbgAPIv2(object):
    '''
    API reference
    https://torrentapi.org/apidocs_v2.txt
    '''
    ENDPOINT = 'http://torrentapi.org/pubapi_v2.php'

    def __init__(self):
        super(_RarbgAPIv2, self).__init__()
        self._endpoint = self.ENDPOINT

    # pylint: disable=no-self-use
    def _requests(self, method, url, params=None):
        sess = requests.Session()
        req = requests.Request(method, url, params=params)
        preq = req.prepare()
        resp = sess.send(preq)
        resp.raise_for_status()
        return resp

    def _get_token(self):
        '''
        {"token":"xxxxx"}
        '''
        params = {
            'get_token': 'get_token'
        }
        return self._requests('GET', self._endpoint, params)

    def _query(self, mode, token=None, **kwargs):
        params = {
            'mode': mode,
            'token': token
        }
        for key, value in kwargs.iteritems():
            if key not in ['search_string', 'sort', 'limit',
                           'category', 'format_']:
                raise ValueError('unsupported parameter %s' % key)

            if value is None:
                continue

            params[key] = value

        return self._requests('GET', self._endpoint, params)

def request(func):
    # pylint: disable=protected-access
    def wrapper(self, *args, **kwargs):
        max_retries = retries = self._options['retries']
        while retries > 0:
            try:
                backoff = 2**(max_retries - retries)
                if not self._bucket.acquire(1, timeout=2):
                    raise ValueError('accquire token timeout')

                if not self._token:
                    raise TokenExpireException('Empty token')

                resp = func(self, token=self._token, *args, **kwargs)
                json_ = resp.json(object_hook=json_hook)
                return json_['torrent_results']
            except TokenExpireException:
                resp = self._get_token()
                content = resp.json()
                self._token = content['token']
                self._log.debug('token=%s', self._token)
            except Exception as exp:  # pylint: disable=broad-except
                self._log.exception('Unexpected exceptin %s', exp)
                retries -= 1
                if not retries:
                    raise
            else:
                retries -= 1
            finally:
                time.sleep(backoff)

        assert 0, 'Unexpected response'
    return wrapper

class RarbgAPI(_RarbgAPIv2):

    CATEGORY_ADULT = 44
    CATEGORY_MOVIE_XVID = 16
    CATEGORY_MOVIE_XVID_720P = 48
    CATEGORY_MOVIE_H264 = 17
    CATEGORY_MOVIE_H264_1080P = 44
    CATEGORY_MOVIE_H264_720P = 45
    CATEGORY_MOVIE_H264_3D = 47
    CATEGORY_MOVIE_FULL_BD = 42
    CATEGORY_MOVIE_BD_REMUX = 46
    CATEGORY_TV_EPISODES = 18
    CATEGORY_TV_EPISODES_HD = 41
    CATEGORY_TV_EPISODES_UHD = 49
    CATEGORY_MUSIC_MP3 = 23
    CATEGORY_MUSIC_FLAC = 25
    CATEGORY_GAMES_PC_ISO = 27
    CATEGORY_GAMES_PC_RIP = 28
    CATEGORY_GAMES_PS3 = 40
    CATEGORY_GAMES_XBOX = 32
    CATEGORY_SOFTWARE = 33
    CATEGORY_EBOOK = 35

    def __init__(self, **options):
        super(RarbgAPI, self).__init__()
        self._token = None
        self._bucket = LeakyBucket(0.5)
        self._log = logging.getLogger(__name__)
        default_options = {
            'retries': 5,
        }
        if options:
            default_options.update(options)
        self._options = default_options

    @request
    # pylint: disable=too-many-arguments
    def list(self, search_string=None, sort=None, limit=None,
             category=None, format_=None, **kwargs):
        return self._query('list', search_string=search_string, sort=sort,
                           limit=limit, category=category, format_=format_,
                           **kwargs)

    @request
    # pylint: disable=too-many-arguments
    def search(self, search_string=None, sort=None, limit=None,
               category=None, format_=None, **kwargs):
        return self._query('search', search_string=search_string,
                           sort=sort, limit=limit, category=category,
                           format_=format_, **kwargs)
<commit_msg>fix typo<commit_after>import time
import logging

import requests

from .leakybucket import LeakyBucket

class TokenExpireException(Exception):
    pass

# pylint: disable=too-many-instance-attributes,too-few-public-methods
class Torrent(object):
    '''
    brief
    {
        "filename":"Off.Piste.2016.iNTERNAL.BDRip.x264-LiBRARiANS",
        "category":"Movies/x264",
        "download":"magnet:..."
    }

    extended
    {
        "title":"Off.Piste.2016.iNTERNAL.BDRip.x264-LiBRARiANS",
        "category":"Movies/x264",
        "download":"magnet:...",
        "seeders":12,
        "leechers":6,
        "size":504519520,
        "pubdate":"2017-05-21 02:13:49 +0000",
        "episode_info":{
            "imdb":"tt4443856",
            "tvrage":null,
            "tvdb":null,
            "themoviedb":"430293"
        },
        "ranked":1,
        "info_page":"https://torrentapi.org/...."
    }
    '''
    def __init__(self, mapping):
        self._raw = mapping
        self.is_extended = 'title' in self._raw
        self.category = self._raw['category']
        self.download = self._raw['download']
        self.filename = self._raw.get('filename') or self._raw.get('title')
        self.size = self._raw.get('size')
        self.pubdate = self._raw.get('pubdate')
        self.page = self._raw.get('info_page')

    def __str__(self):
        return '%s(%s)' % (self.filename, self.category)

def json_hook(dct):
    error_code = dct.get('error_code')
    if error_code in [2, 4]:
        raise TokenExpireException('Token expired')
    if 'download' in dct:
        return Torrent(dct)
    return dct

class _RarbgAPIv2(object):
    '''
    API reference
    https://torrentapi.org/apidocs_v2.txt
    '''
    ENDPOINT = 'http://torrentapi.org/pubapi_v2.php'

    def __init__(self):
        super(_RarbgAPIv2, self).__init__()
        self._endpoint = self.ENDPOINT

    # pylint: disable=no-self-use
    def _requests(self, method, url, params=None):
        sess = requests.Session()
        req = requests.Request(method, url, params=params)
        preq = req.prepare()
        resp = sess.send(preq)
        resp.raise_for_status()
        return resp

    def _get_token(self):
        '''
        {"token":"xxxxx"}
        '''
        params = {
            'get_token': 'get_token'
        }
        return self._requests('GET', self._endpoint, params)

    def _query(self, mode, token=None, **kwargs):
        params = {
            'mode': mode,
            'token': token
        }
        for key, value in kwargs.iteritems():
            if key not in ['search_string', 'sort', 'limit',
                           'category', 'format_']:
                raise ValueError('unsupported parameter %s' % key)

            if value is None:
                continue

            params[key] = value

        return self._requests('GET', self._endpoint, params)

def request(func):
    # pylint: disable=protected-access
    def wrapper(self, *args, **kwargs):
        max_retries = retries = self._options['retries']
        while retries > 0:
            try:
                backoff = 2**(max_retries - retries)
                if not self._bucket.acquire(1, timeout=2):
                    raise ValueError('accquire token timeout')

                if not self._token:
                    raise TokenExpireException('Empty token')

                resp = func(self, token=self._token, *args, **kwargs)
                json_ = resp.json(object_hook=json_hook)
                return json_['torrent_results']
            except TokenExpireException:
                resp = self._get_token()
                content = resp.json()
                self._token = content['token']
                self._log.debug('token=%s', self._token)
            except Exception as exp:  # pylint: disable=broad-except
                self._log.exception('Unexpected exceptin %s', exp)
                retries -= 1
                if not retries:
                    raise
            else:
                retries -= 1
            finally:
                time.sleep(backoff)

        assert 0, 'Unexpected response'
    return wrapper

class RarbgAPI(_RarbgAPIv2):

    CATEGORY_ADULT = 44
    CATEGORY_MOVIE_XVID = 16
    CATEGORY_MOVIE_XVID_720P = 48
    CATEGORY_MOVIE_H264 = 17
    CATEGORY_MOVIE_H264_1080P = 44
    CATEGORY_MOVIE_H264_720P = 45
    CATEGORY_MOVIE_H264_3D = 47
    CATEGORY_MOVIE_FULL_BD = 42
    CATEGORY_MOVIE_BD_REMUX = 46
    CATEGORY_TV_EPISODES = 18
    CATEGORY_TV_EPISODES_HD = 41
    CATEGORY_TV_EPISODES_UHD = 49
    CATEGORY_MUSIC_MP3 = 23
    CATEGORY_MUSIC_FLAC = 25
    CATEGORY_GAMES_PC_ISO = 27
    CATEGORY_GAMES_PC_RIP = 28
    CATEGORY_GAMES_PS3 = 40
    CATEGORY_GAMES_XBOX = 32
    CATEGORY_SOFTWARE = 33
    CATEGORY_EBOOK = 35

    def __init__(self, **options):
        super(RarbgAPI, self).__init__()
        self._token = None
        self._bucket = LeakyBucket(0.5)
        self._log = logging.getLogger(__name__)
        default_options = {
            'retries': 5,
        }
        if options:
            default_options.update(options)
        self._options = default_options

    @request
    # pylint: disable=too-many-arguments
    def list(self, search_string=None, sort=None, limit=None,
             category=None, format_=None, **kwargs):
        return self._query('list', search_string=search_string, sort=sort,
                           limit=limit, category=category, format_=format_,
                           **kwargs)

    @request
    # pylint: disable=too-many-arguments
    def search(self, search_string=None, sort=None, limit=None,
               category=None, format_=None, **kwargs):
        return self._query('search', search_string=search_string,
                           sort=sort, limit=limit, category=category,
                           format_=format_, **kwargs)
<|endoftext|>


<commit_before>import logging
import os
import shutil
from types import TracebackType
from typing import Any, Callable, Dict, List, Text, Optional, Type

import rasa.core.utils
import rasa.utils.io
from rasa.constants import (
    GLOBAL_USER_CONFIG_PATH,
    DEFAULT_LOG_LEVEL,
    ENV_LOG_LEVEL,
    DEFAULT_LOG_LEVEL_LIBRARIES,
    ENV_LOG_LEVEL_LIBRARIES,
)

logger = logging.getLogger(__name__)

class TempDirectoryPath(str):
    """Represents a path to an temporary directory. When used as a context
    manager, it erases the contents of the directory on exit.

    """

    def __enter__(self) -> "TempDirectoryPath":
        return self

    def __exit__(
        self,
        _exc: Optional[Type[BaseException]],
        _value: Optional[Exception],
        _tb: Optional[TracebackType],
    ) -> bool:
        if os.path.exists(self):
            shutil.rmtree(self)

def arguments_of(func: Callable) -> List[Text]:
    """Return the parameters of the function `func` as a list of names."""
    import inspect

    return list(inspect.signature(func).parameters.keys())

def read_global_config() -> Dict[Text, Any]:
    """Read global Rasa configuration."""
    # noinspection PyBroadException
    try:
        return rasa.utils.io.read_config_file(GLOBAL_USER_CONFIG_PATH)
    except Exception:
        # if things go south we pretend there is no config
        return {}

def set_log_level(log_level: Optional[int] = None):
    """Set log level of Rasa and Tensorflow either to the provided log level or
    to the log level specified in the environment variable 'LOG_LEVEL'. If none is set
    a default log level will be used."""
    import logging

    if not log_level:
        log_level = os.environ.get(ENV_LOG_LEVEL, DEFAULT_LOG_LEVEL)
        log_level = logging.getLevelName(log_level)

    logging.getLogger("rasa").setLevel(log_level)

    update_tensorflow_log_level()
    update_asyncio_log_level()
    update_apscheduler_log_level()

    os.environ[ENV_LOG_LEVEL] = logging.getLevelName(log_level)

def update_apscheduler_log_level():
    log_level = os.environ.get(ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES)

    logging.getLogger("apscheduler.scheduler").setLevel(log_level)
    logging.getLogger("apscheduler.scheduler").propagate = False
    logging.getLogger("apscheduler.executors.default").setLevel(log_level)
    logging.getLogger("apscheduler.executors.default").propagate = False

def update_tensorflow_log_level():
    """Set the log level of Tensorflow to the log level specified in the environment
    variable 'LOG_LEVEL_LIBRARIES'."""
    import tensorflow as tf

    log_level = os.environ.get(ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES)

    os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"  # disables AVX2 FMA warnings (CPU support)
    if log_level == "DEBUG":
        tf_log_level = tf.compat.v1.logging.DEBUG
    elif log_level == "INFO":
        tf_log_level = tf.compat.v1.logging.INFO
    elif log_level == "WARNING":
        tf_log_level = tf.compat.v1.logging.WARN
    else:
        tf_log_level = tf.compat.v1.logging.ERROR

    tf.compat.v1.logging.set_verbosity(tf_log_level)
    logging.getLogger("tensorflow").propagate = False

def update_sanic_log_level(log_file: Optional[Text] = None):
    """Set the log level of sanic loggers to the log level specified in the environment
    variable 'LOG_LEVEL_LIBRARIES'."""
    from sanic.log import logger, error_logger, access_logger

    log_level = os.environ.get(ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES)

    logger.setLevel(log_level)
    error_logger.setLevel(log_level)
    access_logger.setLevel(log_level)

    logger.propagate = False
    error_logger.propagate = False
    access_logger.propagate = False

    if log_file is not None:
        formatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s]  %(message)s")
        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(formatter)

        logger.addHandler(file_handler)
        error_logger.addHandler(file_handler)
        access_logger.addHandler(file_handler)

def update_asyncio_log_level():
    """Set the log level of asyncio to the log level specified in the environment
    variable 'LOG_LEVEL_LIBRARIES'."""
    log_level = os.environ.get(ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES)
    logging.getLogger("asyncio").setLevel(log_level)

def obtain_verbosity() -> int:
    """Returns a verbosity level according to the set log level."""
    log_level = os.environ.get(ENV_LOG_LEVEL, DEFAULT_LOG_LEVEL)

    verbosity = 0
    if log_level == "DEBUG":
        verbosity = 2
    if log_level == "INFO":
        verbosity = 1

    return verbosity

def is_logging_disabled() -> bool:
    """Returns true, if log level is set to WARNING or ERROR, false otherwise."""
    log_level = os.environ.get(ENV_LOG_LEVEL, DEFAULT_LOG_LEVEL)

    return log_level == "ERROR" or log_level == "WARNING"

def sort_list_of_dicts_by_first_key(dicts: List[Dict]) -> List[Dict]:
    """Sorts a list of dictionaries by their first key."""
    return sorted(dicts, key=lambda d: list(d.keys())[0])

# noinspection PyUnresolvedReferences
def class_from_module_path(
    module_path: Text, lookup_path: Optional[Text] = None
) -> Any:
    """Given the module name and path of a class, tries to retrieve the class.

    The loaded class can be used to instantiate new objects. """
    import importlib

    # load the module, will raise ImportError if module cannot be loaded
    if "." in module_path:
        module_name, _, class_name = module_path.rpartition(".")
        m = importlib.import_module(module_name)
        # get the class, will raise AttributeError if class cannot be found
        return getattr(m, class_name)
    else:
        module = globals().get(module_path, locals().get(module_path))
        if module is not None:
            return module

        if lookup_path:
            # last resort: try to import the class from the lookup path
            m = importlib.import_module(lookup_path)
            return getattr(m, module_path)
        else:
            raise ImportError("Cannot retrieve class from path {}.".format(module_path))

def minimal_kwargs(
    kwargs: Dict[Text, Any], func: Callable, excluded_keys: Optional[List] = None
) -> Dict[Text, Any]:
    """Returns only the kwargs which are required by a function. Keys, contained in
    the exception list, are not included.

    Args:
        kwargs: All available kwargs.
        func: The function which should be called.
        excluded_keys: Keys to exclude from the result.

    Returns:
        Subset of kwargs which are accepted by `func`.

    """

    excluded_keys = excluded_keys or []

    possible_arguments = arguments_of(func)

    return {
        k: v
        for k, v in kwargs.items()
        if k in possible_arguments and k not in excluded_keys
    }

def write_global_config_value(name: Text, value: Any) -> None:
    """Read global Rasa configuration."""

    try:
        os.makedirs(os.path.dirname(GLOBAL_USER_CONFIG_PATH), exist_ok=True)

        c = read_global_config()
        c[name] = value
        rasa.core.utils.dump_obj_as_yaml_to_file(GLOBAL_USER_CONFIG_PATH, c)
    except Exception as e:
        logger.warning(
            "Failed to write global config. Error: {}. Skipping." "".format(e)
        )

def read_global_config_value(name: Text, unavailable_ok: bool = True) -> Any:
    """Read a value from the global Rasa configuration."""

    def not_found():
        if unavailable_ok:
            return None
        else:
            raise ValueError("Configuration '{}' key not found.".format(name))

    if not os.path.exists(GLOBAL_USER_CONFIG_PATH):
        return not_found()

    c = read_global_config()

    if name in c:
        return c[name]
    else:
        return not_found()

def mark_as_experimental_feature(feature_name: Text) -> None:
    """Warns users that they are using an experimental feature."""

    logger.warning(
        "The {} is currently experimental and might change or be "
        "removed in the future 🔬 Please share your feedback on it in the "
        "forum (https://forum.rasa.com) to help us make this feature "
        "ready for production.".format(feature_name)
    )

def lazy_property(function: Callable) -> Any:
    """Allows to avoid recomputing a property over and over.

    The result gets stored in a local var. Computation of the property
    will happen once, on the first call of the property. All
    succeeding calls will use the value stored in the private property."""

    attr_name = "_lazy_" + function.__name__

    @property
    def _lazyprop(self):
        if not hasattr(self, attr_name):
            setattr(self, attr_name, function(self))
        return getattr(self, attr_name)

    return _lazyprop
<commit_msg>set apscheduler loggers in loop<commit_after>import logging
import os
import shutil
from types import TracebackType
from typing import Any, Callable, Dict, List, Text, Optional, Type

import rasa.core.utils
import rasa.utils.io
from rasa.constants import (
    GLOBAL_USER_CONFIG_PATH,
    DEFAULT_LOG_LEVEL,
    ENV_LOG_LEVEL,
    DEFAULT_LOG_LEVEL_LIBRARIES,
    ENV_LOG_LEVEL_LIBRARIES,
)

logger = logging.getLogger(__name__)

class TempDirectoryPath(str):
    """Represents a path to an temporary directory. When used as a context
    manager, it erases the contents of the directory on exit.

    """

    def __enter__(self) -> "TempDirectoryPath":
        return self

    def __exit__(
        self,
        _exc: Optional[Type[BaseException]],
        _value: Optional[Exception],
        _tb: Optional[TracebackType],
    ) -> bool:
        if os.path.exists(self):
            shutil.rmtree(self)

def arguments_of(func: Callable) -> List[Text]:
    """Return the parameters of the function `func` as a list of names."""
    import inspect

    return list(inspect.signature(func).parameters.keys())

def read_global_config() -> Dict[Text, Any]:
    """Read global Rasa configuration."""
    # noinspection PyBroadException
    try:
        return rasa.utils.io.read_config_file(GLOBAL_USER_CONFIG_PATH)
    except Exception:
        # if things go south we pretend there is no config
        return {}

def set_log_level(log_level: Optional[int] = None):
    """Set log level of Rasa and Tensorflow either to the provided log level or
    to the log level specified in the environment variable 'LOG_LEVEL'. If none is set
    a default log level will be used."""
    import logging

    if not log_level:
        log_level = os.environ.get(ENV_LOG_LEVEL, DEFAULT_LOG_LEVEL)
        log_level = logging.getLevelName(log_level)

    logging.getLogger("rasa").setLevel(log_level)

    update_tensorflow_log_level()
    update_asyncio_log_level()
    update_apscheduler_log_level()

    os.environ[ENV_LOG_LEVEL] = logging.getLevelName(log_level)

def update_apscheduler_log_level():
    log_level = os.environ.get(ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES)

    apscheduler_loggers = [
        "apscheduler",
        "apscheduler.scheduler",
        "apscheduler.executors.default",
    ]

    for logger_name in apscheduler_loggers:
        logging.getLogger(logger_name).setLevel(log_level)
        logging.getLogger(logger_name).propagate = False

def update_tensorflow_log_level():
    """Set the log level of Tensorflow to the log level specified in the environment
    variable 'LOG_LEVEL_LIBRARIES'."""
    import tensorflow as tf

    log_level = os.environ.get(ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES)

    os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"  # disables AVX2 FMA warnings (CPU support)
    if log_level == "DEBUG":
        tf_log_level = tf.compat.v1.logging.DEBUG
    elif log_level == "INFO":
        tf_log_level = tf.compat.v1.logging.INFO
    elif log_level == "WARNING":
        tf_log_level = tf.compat.v1.logging.WARN
    else:
        tf_log_level = tf.compat.v1.logging.ERROR

    tf.compat.v1.logging.set_verbosity(tf_log_level)
    logging.getLogger("tensorflow").propagate = False

def update_sanic_log_level(log_file: Optional[Text] = None):
    """Set the log level of sanic loggers to the log level specified in the environment
    variable 'LOG_LEVEL_LIBRARIES'."""
    from sanic.log import logger, error_logger, access_logger

    log_level = os.environ.get(ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES)

    logger.setLevel(log_level)
    error_logger.setLevel(log_level)
    access_logger.setLevel(log_level)

    logger.propagate = False
    error_logger.propagate = False
    access_logger.propagate = False

    if log_file is not None:
        formatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s]  %(message)s")
        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(formatter)

        logger.addHandler(file_handler)
        error_logger.addHandler(file_handler)
        access_logger.addHandler(file_handler)

def update_asyncio_log_level():
    """Set the log level of asyncio to the log level specified in the environment
    variable 'LOG_LEVEL_LIBRARIES'."""
    log_level = os.environ.get(ENV_LOG_LEVEL_LIBRARIES, DEFAULT_LOG_LEVEL_LIBRARIES)
    logging.getLogger("asyncio").setLevel(log_level)

def obtain_verbosity() -> int:
    """Returns a verbosity level according to the set log level."""
    log_level = os.environ.get(ENV_LOG_LEVEL, DEFAULT_LOG_LEVEL)

    verbosity = 0
    if log_level == "DEBUG":
        verbosity = 2
    if log_level == "INFO":
        verbosity = 1

    return verbosity

def is_logging_disabled() -> bool:
    """Returns true, if log level is set to WARNING or ERROR, false otherwise."""
    log_level = os.environ.get(ENV_LOG_LEVEL, DEFAULT_LOG_LEVEL)

    return log_level == "ERROR" or log_level == "WARNING"

def sort_list_of_dicts_by_first_key(dicts: List[Dict]) -> List[Dict]:
    """Sorts a list of dictionaries by their first key."""
    return sorted(dicts, key=lambda d: list(d.keys())[0])

# noinspection PyUnresolvedReferences
def class_from_module_path(
    module_path: Text, lookup_path: Optional[Text] = None
) -> Any:
    """Given the module name and path of a class, tries to retrieve the class.

    The loaded class can be used to instantiate new objects. """
    import importlib

    # load the module, will raise ImportError if module cannot be loaded
    if "." in module_path:
        module_name, _, class_name = module_path.rpartition(".")
        m = importlib.import_module(module_name)
        # get the class, will raise AttributeError if class cannot be found
        return getattr(m, class_name)
    else:
        module = globals().get(module_path, locals().get(module_path))
        if module is not None:
            return module

        if lookup_path:
            # last resort: try to import the class from the lookup path
            m = importlib.import_module(lookup_path)
            return getattr(m, module_path)
        else:
            raise ImportError("Cannot retrieve class from path {}.".format(module_path))

def minimal_kwargs(
    kwargs: Dict[Text, Any], func: Callable, excluded_keys: Optional[List] = None
) -> Dict[Text, Any]:
    """Returns only the kwargs which are required by a function. Keys, contained in
    the exception list, are not included.

    Args:
        kwargs: All available kwargs.
        func: The function which should be called.
        excluded_keys: Keys to exclude from the result.

    Returns:
        Subset of kwargs which are accepted by `func`.

    """

    excluded_keys = excluded_keys or []

    possible_arguments = arguments_of(func)

    return {
        k: v
        for k, v in kwargs.items()
        if k in possible_arguments and k not in excluded_keys
    }

def write_global_config_value(name: Text, value: Any) -> None:
    """Read global Rasa configuration."""

    try:
        os.makedirs(os.path.dirname(GLOBAL_USER_CONFIG_PATH), exist_ok=True)

        c = read_global_config()
        c[name] = value
        rasa.core.utils.dump_obj_as_yaml_to_file(GLOBAL_USER_CONFIG_PATH, c)
    except Exception as e:
        logger.warning(
            "Failed to write global config. Error: {}. Skipping." "".format(e)
        )

def read_global_config_value(name: Text, unavailable_ok: bool = True) -> Any:
    """Read a value from the global Rasa configuration."""

    def not_found():
        if unavailable_ok:
            return None
        else:
            raise ValueError("Configuration '{}' key not found.".format(name))

    if not os.path.exists(GLOBAL_USER_CONFIG_PATH):
        return not_found()

    c = read_global_config()

    if name in c:
        return c[name]
    else:
        return not_found()

def mark_as_experimental_feature(feature_name: Text) -> None:
    """Warns users that they are using an experimental feature."""

    logger.warning(
        "The {} is currently experimental and might change or be "
        "removed in the future 🔬 Please share your feedback on it in the "
        "forum (https://forum.rasa.com) to help us make this feature "
        "ready for production.".format(feature_name)
    )

def lazy_property(function: Callable) -> Any:
    """Allows to avoid recomputing a property over and over.

    The result gets stored in a local var. Computation of the property
    will happen once, on the first call of the property. All
    succeeding calls will use the value stored in the private property."""

    attr_name = "_lazy_" + function.__name__

    @property
    def _lazyprop(self):
        if not hasattr(self, attr_name):
            setattr(self, attr_name, function(self))
        return getattr(self, attr_name)

    return _lazyprop
<|endoftext|>


<commit_before>from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

__version__ = '0.7.0a3'
<commit_msg>increased version<commit_after>from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

__version__ = '0.7.0a4'
<|endoftext|>