"""setuptools.command.egg_info

Create a distribution's .egg-info directory and contents"""

# This module should be kept compatible with Python 2.3
import os, re, sys
from setuptools import Command
from distutils.errors import *
from distutils import log
from setuptools.command.sdist import sdist
from distutils.util import convert_path
from distutils.filelist import FileList as _FileList
from pkg_resources import parse_requirements, safe_name, parse_version, \
    safe_version, yield_lines, EntryPoint, iter_entry_points, to_filename
from sdist import walk_revctrl

class egg_info(Command):
    description = "create a distribution's .egg-info directory"

    user_options = [
        ('egg-base=', 'e', "directory containing .egg-info directories"
                           " (default: top of the source tree)"),
        ('tag-svn-revision', 'r',
            "Add subversion revision ID to version number"),
        ('tag-date', 'd', "Add date stamp (e.g. 20050528) to version number"),
        ('tag-build=', 'b', "Specify explicit tag to add to version number"),
        ('no-svn-revision', 'R',
            "Don't add subversion revision ID [default]"),
        ('no-date', 'D', "Don't include date stamp [default]"),
    ]

    boolean_options = ['tag-date', 'tag-svn-revision']
    negative_opt = {'no-svn-revision': 'tag-svn-revision',
                    'no-date': 'tag-date'}

    def initialize_options(self):
        self.egg_name = None
        self.egg_version = None
        self.egg_base = None
        self.egg_info = None
        self.tag_build = None
        self.tag_svn_revision = 0
        self.tag_date = 0
        self.broken_egg_info = False
        self.vtags = None

    def save_version_info(self, filename):
        from setopt import edit_config
        edit_config(
            filename,
            {'egg_info':
                {'tag_svn_revision':0, 'tag_date': 0, 'tag_build': self.tags()}
            }
        )

    def finalize_options (self):
        self.egg_name = safe_name(self.distribution.get_name())
        self.vtags = self.tags()
        self.egg_version = self.tagged_version()

        try:
            list(
                parse_requirements('%s==%s' % (self.egg_name,self.egg_version))
            )
        except ValueError:
            raise DistutilsOptionError(
                "Invalid distribution name or version syntax: %s-%s" %
                (self.egg_name,self.egg_version)
            )

        if self.egg_base is None:
            dirs = self.distribution.package_dir
            self.egg_base = (dirs or {}).get('',os.curdir)

        self.ensure_dirname('egg_base')
        self.egg_info = to_filename(self.egg_name)+'.egg-info'
        if self.egg_base != os.curdir:
            self.egg_info = os.path.join(self.egg_base, self.egg_info)
        if '-' in self.egg_name: self.check_broken_egg_info()

        # Set package version for the benefit of dumber commands
        # (e.g. sdist, bdist_wininst, etc.)
        #
        self.distribution.metadata.version = self.egg_version

        # If we bootstrapped around the lack of a PKG-INFO, as might be the
        # case in a fresh checkout, make sure that any special tags get added
        # to the version info
        #
        pd = self.distribution._patched_dist
        if pd is not None and pd.key==self.egg_name.lower():
            pd._version = self.egg_version
            pd._parsed_version = parse_version(self.egg_version)
            self.distribution._patched_dist = None

    def write_or_delete_file(self, what, filename, data, force=False):
        """Write `data` to `filename` or delete if empty

        If `data` is non-empty, this routine is the same as ``write_file()``.
        If `data` is empty but not ``None``, this is the same as calling
        ``delete_file(filename)`.  If `data` is ``None``, then this is a no-op
        unless `filename` exists, in which case a warning is issued about the
        orphaned file (if `force` is false), or deleted (if `force` is true).
        """
        if data:
            self.write_file(what, filename, data)
        elif os.path.exists(filename):
            if data is None and not force:
                log.warn(
                    "%s not set in setup(), but %s exists", what, filename
                )
                return
            else:
                self.delete_file(filename)

    def write_file(self, what, filename, data):
        """Write `data` to `filename` (if not a dry run) after announcing it

        `what` is used in a log message to identify what is being written
        to the file.
        """
        log.info("writing %s to %s", what, filename)
        if sys.version_info >= (3,):
            data = data.encode("utf-8")
        if not self.dry_run:
            f = open(filename, 'wb')
            f.write(data)
            f.close()

    def delete_file(self, filename):
        """Delete `filename` (if not a dry run) after announcing it"""
        log.info("deleting %s", filename)
        if not self.dry_run:
            os.unlink(filename)

    def tagged_version(self):
        version = self.distribution.get_version()
        # egg_info may be called more than once for a distribution,
        # in which case the version string already contains all tags.
        if self.vtags and version.endswith(self.vtags):
            return safe_version(version)
        return safe_version(version + self.vtags)

    def run(self):
        self.mkpath(self.egg_info)
        installer = self.distribution.fetch_build_egg
        for ep in iter_entry_points('egg_info.writers'):
            writer = ep.load(installer=installer)
            writer(self, ep.name, os.path.join(self.egg_info,ep.name))

        # Get rid of native_libs.txt if it was put there by older bdist_egg
        nl = os.path.join(self.egg_info, "native_libs.txt")
        if os.path.exists(nl):
            self.delete_file(nl)

        self.find_sources()

    def tags(self):
        version = ''
        if self.tag_build:
            version+=self.tag_build
        if self.tag_svn_revision and (
            os.path.exists('.svn') or os.path.exists('PKG-INFO')
        ):  version += '-r%s' % self.get_svn_revision()
        if self.tag_date:
            import time; version += time.strftime("-%Y%m%d")
        return version

    def get_svn_revision(self):
        revision = 0
        urlre = re.compile('url="([^"]+)"')
        revre = re.compile('committed-rev="(\d+)"')

        for base,dirs,files in os.walk(os.curdir):
            if '.svn' not in dirs:
                dirs[:] = []
                continue    # no sense walking uncontrolled subdirs
            dirs.remove('.svn')
            f = open(os.path.join(base,'.svn','entries'))
            data = f.read()
            f.close()

            if data.startswith('10') or data.startswith('9') or data.startswith('8'):
                data = map(str.splitlines,data.split('\n\x0c\n'))
                del data[0][0]  # get rid of the '8' or '9' or '10'
                dirurl = data[0][3]
                localrev = max([int(d[9]) for d in data if len(d)>9 and d[9]]+[0])
            elif data.startswith('<?xml'):
                dirurl = urlre.search(data).group(1)    # get repository URL
                localrev = max([int(m.group(1)) for m in revre.finditer(data)]+[0])
            else:
                log.warn("unrecognized .svn/entries format; skipping %s", base)
                dirs[:] = []
                continue
            if base==os.curdir:
                base_url = dirurl+'/'   # save the root url
            elif not dirurl.startswith(base_url):
                dirs[:] = []
                continue    # not part of the same svn tree, skip it
            revision = max(revision, localrev)

        return str(revision or get_pkg_info_revision())

    def find_sources(self):
        """Generate SOURCES.txt manifest file"""
        manifest_filename = os.path.join(self.egg_info,"SOURCES.txt")
        mm = manifest_maker(self.distribution)
        mm.manifest = manifest_filename
        mm.run()
        self.filelist = mm.filelist

    def check_broken_egg_info(self):
        bei = self.egg_name+'.egg-info'
        if self.egg_base != os.curdir:
            bei = os.path.join(self.egg_base, bei)
        if os.path.exists(bei):
            log.warn(
                "-"*78+'\n'
                "Note: Your current .egg-info directory has a '-' in its name;"
                '\nthis will not work correctly with "setup.py develop".\n\n'
                'Please rename %s to %s to correct this problem.\n'+'-'*78,
                bei, self.egg_info
            )
            self.broken_egg_info = self.egg_info
            self.egg_info = bei     # make it work for now

class FileList(_FileList):
    """File list that accepts only existing, platform-independent paths"""

    def append(self, item):
        if item.endswith('\r'):     # Fix older sdists built on Windows
            item = item[:-1]
        path = convert_path(item)

        if sys.version_info >= (3,):
            try:
                if os.path.exists(path) or os.path.exists(path.encode('utf-8')):
                    self.files.append(path)
            except UnicodeEncodeError:
                # Accept UTF-8 filenames even if LANG=C
                if os.path.exists(path.encode('utf-8')):
                    self.files.append(path)
                else:
                    log.warn("'%s' not %s encodable -- skipping", path,
                        sys.getfilesystemencoding())
        else:
            if os.path.exists(path):
                self.files.append(path)

class manifest_maker(sdist):

    template = "MANIFEST.in"

    def initialize_options (self):
        self.use_defaults = 1
        self.prune = 1
        self.manifest_only = 1
        self.force_manifest = 1

    def finalize_options(self):
        pass

    def run(self):
        self.filelist = FileList()
        if not os.path.exists(self.manifest):
            self.write_manifest()   # it must exist so it'll get in the list
        self.filelist.findall()
        self.add_defaults()
        if os.path.exists(self.template):
            self.read_template()
        self.prune_file_list()
        self.filelist.sort()
        self.filelist.remove_duplicates()
        self.write_manifest()

    def write_manifest (self):
        """Write the file list in 'self.filelist' (presumably as filled in
        by 'add_defaults()' and 'read_template()') to the manifest file
        named by 'self.manifest'.
        """
        # The manifest must be UTF-8 encodable. See #303.
        if sys.version_info >= (3,):
            files = []
            for file in self.filelist.files:
                try:
                    file.encode("utf-8")
                except UnicodeEncodeError:
                    log.warn("'%s' not UTF-8 encodable -- skipping" % file)
                else:
                    files.append(file)
            self.filelist.files = files

        files = self.filelist.files
        if os.sep!='/':
            files = [f.replace(os.sep,'/') for f in files]
        self.execute(write_file, (self.manifest, files),
                     "writing manifest file '%s'" % self.manifest)

    def warn(self, msg):    # suppress missing-file warnings from sdist
        if not msg.startswith("standard file not found:"):
            sdist.warn(self, msg)

    def add_defaults(self):
        sdist.add_defaults(self)
        self.filelist.append(self.template)
        self.filelist.append(self.manifest)
        rcfiles = list(walk_revctrl())
        if rcfiles:
            self.filelist.extend(rcfiles)
        elif os.path.exists(self.manifest):
            self.read_manifest()
        ei_cmd = self.get_finalized_command('egg_info')
        self.filelist.include_pattern("*", prefix=ei_cmd.egg_info)

    def prune_file_list (self):
        build = self.get_finalized_command('build')
        base_dir = self.distribution.get_fullname()
        self.filelist.exclude_pattern(None, prefix=build.build_base)
        self.filelist.exclude_pattern(None, prefix=base_dir)
        sep = re.escape(os.sep)
        self.filelist.exclude_pattern(sep+r'(RCS|CVS|\.svn)'+sep, is_regex=1)

def write_file (filename, contents):
    """Create a file with the specified name and write 'contents' (a
    sequence of strings without line terminators) to it.
    """
    contents = "\n".join(contents)
    if sys.version_info >= (3,):
        contents = contents.encode("utf-8")
    f = open(filename, "wb")        # always write POSIX-style manifest
    f.write(contents)
    f.close()

def write_pkg_info(cmd, basename, filename):
    log.info("writing %s", filename)
    if not cmd.dry_run:
        metadata = cmd.distribution.metadata
        metadata.version, oldver = cmd.egg_version, metadata.version
        metadata.name, oldname   = cmd.egg_name, metadata.name
        try:
            # write unescaped data to PKG-INFO, so older pkg_resources
            # can still parse it
            metadata.write_pkg_info(cmd.egg_info)
        finally:
            metadata.name, metadata.version = oldname, oldver

        safe = getattr(cmd.distribution,'zip_safe',None)
        import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe)

def warn_depends_obsolete(cmd, basename, filename):
    if os.path.exists(filename):
        log.warn(
            "WARNING: 'depends.txt' is not used by setuptools 0.6!\n"
            "Use the install_requires/extras_require setup() args instead."
        )

def write_requirements(cmd, basename, filename):
    dist = cmd.distribution
    data = ['\n'.join(yield_lines(dist.install_requires or ()))]
    for extra,reqs in (dist.extras_require or {}).items():
        data.append('\n\n[%s]\n%s' % (extra, '\n'.join(yield_lines(reqs))))
    cmd.write_or_delete_file("requirements", filename, ''.join(data))

def write_toplevel_names(cmd, basename, filename):
    pkgs = dict.fromkeys(
        [k.split('.',1)[0]
            for k in cmd.distribution.iter_distribution_names()
        ]
    )
    cmd.write_file("top-level names", filename, '\n'.join(pkgs)+'\n')

def overwrite_arg(cmd, basename, filename):
    write_arg(cmd, basename, filename, True)

def write_arg(cmd, basename, filename, force=False):
    argname = os.path.splitext(basename)[0]
    value = getattr(cmd.distribution, argname, None)
    if value is not None:
        value = '\n'.join(value)+'\n'
    cmd.write_or_delete_file(argname, filename, value, force)

def write_entries(cmd, basename, filename):
    ep = cmd.distribution.entry_points

    if isinstance(ep,basestring) or ep is None:
        data = ep
    elif ep is not None:
        data = []
        for section, contents in ep.items():
            if not isinstance(contents,basestring):
                contents = EntryPoint.parse_group(section, contents)
                contents = '\n'.join(map(str,contents.values()))
            data.append('[%s]\n%s\n\n' % (section,contents))
        data = ''.join(data)

    cmd.write_or_delete_file('entry points', filename, data, True)

def get_pkg_info_revision():
    # See if we can get a -r### off of PKG-INFO, in case this is an sdist of
    # a subversion revision
    #
    if os.path.exists('PKG-INFO'):
        f = open('PKG-INFO','rU')
        for line in f:
            match = re.match(r"Version:.*-r(\d+)\s*$", line)
            if match:
                return int(match.group(1))
        f.close()
    return 0

#


""" Provides bracket matching for Q[Plain]TextEdit widgets.
"""

# System library imports
from IPython.external.qt import QtCore, QtGui

class BracketMatcher(QtCore.QObject):
    """ Matches square brackets, braces, and parentheses based on cursor
        position.
    """

    # Protected class variables.
    _opening_map = { '(':')', '{':'}', '[':']' }
    _closing_map = { ')':'(', '}':'{', ']':'[' }

    #--------------------------------------------------------------------------
    # 'QObject' interface
    #--------------------------------------------------------------------------

    def __init__(self, text_edit):
        """ Create a call tip manager that is attached to the specified Qt
            text edit widget.
        """
        assert isinstance(text_edit, (QtGui.QTextEdit, QtGui.QPlainTextEdit))
        super(BracketMatcher, self).__init__()

        # The format to apply to matching brackets.
        self.format = QtGui.QTextCharFormat()
        self.format.setBackground(QtGui.QColor('silver'))

        self._text_edit = text_edit
        text_edit.cursorPositionChanged.connect(self._cursor_position_changed)

    #--------------------------------------------------------------------------
    # Protected interface
    #--------------------------------------------------------------------------

    def _find_match(self, position):
        """ Given a valid position in the text document, try to find the
            position of the matching bracket. Returns -1 if unsuccessful.
        """
        # Decide what character to search for and what direction to search in.
        document = self._text_edit.document()
        start_char = document.characterAt(position)
        search_char = self._opening_map.get(start_char)
        if search_char:
            increment = 1
        else:
            search_char = self._closing_map.get(start_char)
            if search_char:
                increment = -1
            else:
                return -1

        # Search for the character.
        char = start_char
        depth = 0
        while position >= 0 and position < document.characterCount():
            if char == start_char:
                depth += 1
            elif char == search_char:
                depth -= 1
            if depth == 0:
                break
            position += increment
            char = document.characterAt(position)
        else:
            position = -1
        return position

    def _selection_for_character(self, position):
        """ Convenience method for selecting a character.
        """
        selection = QtGui.QTextEdit.ExtraSelection()
        cursor = self._text_edit.textCursor()
        cursor.setPosition(position)
        cursor.movePosition(QtGui.QTextCursor.NextCharacter,
                            QtGui.QTextCursor.KeepAnchor)
        selection.cursor = cursor
        selection.format = self.format
        return selection

    #------ Signal handlers ----------------------------------------------------

    def _cursor_position_changed(self):
        """ Updates the document formatting based on the new cursor position.
        """
        # Clear out the old formatting.
        self._text_edit.setExtraSelections([])

        # Attempt to match a bracket for the new cursor position.
        cursor = self._text_edit.textCursor()
        if not cursor.hasSelection():
            position = cursor.position() - 1
            match_position = self._find_match(position)
            if match_position != -1:
                extra_selections = [ self._selection_for_character(pos)
                                     for pos in (position, match_position) ]
                self._text_edit.setExtraSelections(extra_selections)


"""
Test Suites
-----------

Provides a LazySuite, which is a suite whose test list is a generator
function, and ContextSuite,which can run fixtures (setup/teardown
functions or methods) for the context that contains its tests.

"""
from __future__ import generators

import logging
import sys
import unittest
from nose.case import Test
from nose.config import Config
from nose.proxy import ResultProxyFactory
from nose.util import isclass, resolve_name, try_run

if sys.platform == 'cli':
    if sys.version_info[:2] < (2, 6):
        import clr
        clr.AddReference("IronPython")
        from IronPython.Runtime.Exceptions import StringException
    else:
        class StringException(Exception):
            pass

log = logging.getLogger(__name__)
#log.setLevel(logging.DEBUG)

# Singleton for default value -- see ContextSuite.__init__ below
_def = object()

def _strclass(cls):
    return "%s.%s" % (cls.__module__, cls.__name__)

class MixedContextError(Exception):
    """Error raised when a context suite sees tests from more than
    one context.
    """
    pass

class LazySuite(unittest.TestSuite):
    """A suite that may use a generator as its list of tests
    """
    def __init__(self, tests=()):
        """Initialize the suite. tests may be an iterable or a generator
        """
        self._set_tests(tests)

    def __iter__(self):
        return iter(self._tests)

    def __repr__(self):
        return "<%s tests=generator (%s)>" % (
            _strclass(self.__class__), id(self))

    def __hash__(self):
        return object.__hash__(self)

    __str__ = __repr__

    def addTest(self, test):
        self._precache.append(test)

    # added to bypass run changes in 2.7's unittest
    def run(self, result):
        for test in self._tests:
            if result.shouldStop:
                break
            test(result)
        return result

    def __nonzero__(self):
        log.debug("tests in %s?", id(self))
        if self._precache:
            return True
        if self.test_generator is None:
            return False
        try:
            test = self.test_generator.next()
            if test is not None:
                self._precache.append(test)
                return True
        except StopIteration:
            pass
        return False

    def _get_tests(self):
        log.debug("precache is %s", self._precache)
        for test in self._precache:
            yield test
        if self.test_generator is None:
            return
        for test in self.test_generator:
            yield test

    def _set_tests(self, tests):
        self._precache = []
        is_suite = isinstance(tests, unittest.TestSuite)
        if callable(tests) and not is_suite:
            self.test_generator = tests()
        elif is_suite:
            # Suites need special treatment: they must be called like
            # tests for their setup/teardown to run (if any)
            self.addTests([tests])
            self.test_generator = None
        else:
            self.addTests(tests)
            self.test_generator = None

    _tests = property(_get_tests, _set_tests, None,
                      "Access the tests in this suite. Access is through a "
                      "generator, so iteration may not be repeatable.")

class ContextSuite(LazySuite):
    """A suite with context.

    A ContextSuite executes fixtures (setup and teardown functions or
    methods) for the context containing its tests.

    The context may be explicitly passed. If it is not, a context (or
    nested set of contexts) will be constructed by examining the tests
    in the suite.
    """
    failureException = unittest.TestCase.failureException
    was_setup = False
    was_torndown = False
    classSetup = ('setup_class', 'setup_all', 'setupClass', 'setupAll',
                     'setUpClass', 'setUpAll')
    classTeardown = ('teardown_class', 'teardown_all', 'teardownClass',
                     'teardownAll', 'tearDownClass', 'tearDownAll')
    moduleSetup = ('setup_module', 'setupModule', 'setUpModule', 'setup',
                   'setUp')
    moduleTeardown = ('teardown_module', 'teardownModule', 'tearDownModule',
                      'teardown', 'tearDown')
    packageSetup = ('setup_package', 'setupPackage', 'setUpPackage')
    packageTeardown = ('teardown_package', 'teardownPackage',
                       'tearDownPackage')

    def __init__(self, tests=(), context=None, factory=None,
                 config=None, resultProxy=None, can_split=True):
        log.debug("Context suite for %s (%s) (%s)", tests, context, id(self))
        self.context = context
        self.factory = factory
        if config is None:
            config = Config()
        self.config = config
        self.resultProxy = resultProxy
        self.has_run = False
        self.can_split = can_split
        self.error_context = None
        LazySuite.__init__(self, tests)

    def __repr__(self):
        return "<%s context=%s>" % (
            _strclass(self.__class__),
            getattr(self.context, '__name__', self.context))
    __str__ = __repr__

    def id(self):
        if self.error_context:
            return '%s:%s' % (repr(self), self.error_context)
        else:
            return repr(self)

    def __hash__(self):
        return object.__hash__(self)

    # 2.3 compat -- force 2.4 call sequence
    def __call__(self, *arg, **kw):
        return self.run(*arg, **kw)

    def exc_info(self):
        """Hook for replacing error tuple output
        """
        return sys.exc_info()

    def _exc_info(self):
        """Bottleneck to fix up IronPython string exceptions
        """
        e = self.exc_info()
        if sys.platform == 'cli':
            if isinstance(e[0], StringException):
                # IronPython throws these StringExceptions, but
                # traceback checks type(etype) == str. Make a real
                # string here.
                e = (str(e[0]), e[1], e[2])

        return e

    def run(self, result):
        """Run tests in suite inside of suite fixtures.
        """
        # proxy the result for myself
        log.debug("suite %s (%s) run called, tests: %s", id(self), self, self._tests)
        #import pdb
        #pdb.set_trace()
        if self.resultProxy:
            result, orig = self.resultProxy(result, self), result
        else:
            result, orig = result, result
        try:
            self.setUp()
        except KeyboardInterrupt:
            raise
        except:
            self.error_context = 'setup'
            result.addError(self, self._exc_info())
            return
        try:
            for test in self._tests:
                if result.shouldStop:
                    log.debug("stopping")
                    break
                # each nose.case.Test will create its own result proxy
                # so the cases need the original result, to avoid proxy
                # chains
                test(orig)
        finally:
            self.has_run = True
            try:
                self.tearDown()
            except KeyboardInterrupt:
                raise
            except:
                self.error_context = 'teardown'
                result.addError(self, self._exc_info())

    def hasFixtures(self, ctx_callback=None):
        context = self.context
        if context is None:
            return False
        if self.implementsAnyFixture(context, ctx_callback=ctx_callback):
            return True
        # My context doesn't have any, but its ancestors might
        factory = self.factory
        if factory:
            ancestors = factory.context.get(self, [])
            for ancestor in ancestors:
                if self.implementsAnyFixture(
                    ancestor, ctx_callback=ctx_callback):
                    return True
        return False

    def implementsAnyFixture(self, context, ctx_callback):
        if isclass(context):
            names = self.classSetup + self.classTeardown
        else:
            names = self.moduleSetup + self.moduleTeardown
            if hasattr(context, '__path__'):
                names += self.packageSetup + self.packageTeardown
        # If my context has any fixture attribute, I have fixtures
        fixt = False
        for m in names:
            if hasattr(context, m):
                fixt = True
                break
        if ctx_callback is None:
            return fixt
        return ctx_callback(context, fixt)

    def setUp(self):
        log.debug("suite %s setUp called, tests: %s", id(self), self._tests)
        if not self:
            # I have no tests
            log.debug("suite %s has no tests", id(self))
            return
        if self.was_setup:
            log.debug("suite %s already set up", id(self))
            return
        context = self.context
        if context is None:
            return
        # before running my own context's setup, I need to
        # ask the factory if my context's contexts' setups have been run
        factory = self.factory
        if factory:
            # get a copy, since we'll be destroying it as we go
            ancestors = factory.context.get(self, [])[:]
            while ancestors:
                ancestor = ancestors.pop()
                log.debug("ancestor %s may need setup", ancestor)
                if ancestor in factory.was_setup:
                    continue
                log.debug("ancestor %s does need setup", ancestor)
                self.setupContext(ancestor)
            if not context in factory.was_setup:
                self.setupContext(context)
        else:
            self.setupContext(context)
        self.was_setup = True
        log.debug("completed suite setup")

    def setupContext(self, context):
        self.config.plugins.startContext(context)
        log.debug("%s setup context %s", self, context)
        if self.factory:
            if context in self.factory.was_setup:
                return
            # note that I ran the setup for this context, so that I'll run
            # the teardown in my teardown
            self.factory.was_setup[context] = self
        if isclass(context):
            names = self.classSetup
        else:
            names = self.moduleSetup
            if hasattr(context, '__path__'):
                names = self.packageSetup + names
        try_run(context, names)

    def shortDescription(self):
        if self.context is None:
            return "test suite"
        return "test suite for %s" % self.context

    def tearDown(self):
        log.debug('context teardown')
        if not self.was_setup or self.was_torndown:
            log.debug(
                "No reason to teardown (was_setup? %s was_torndown? %s)"
                % (self.was_setup, self.was_torndown))
            return
        self.was_torndown = True
        context = self.context
        if context is None:
            log.debug("No context to tear down")
            return

        # for each ancestor... if the ancestor was setup
        # and I did the setup, I can do teardown
        factory = self.factory
        if factory:
            ancestors = factory.context.get(self, []) + [context]
            for ancestor in ancestors:
                log.debug('ancestor %s may need teardown', ancestor)
                if not ancestor in factory.was_setup:
                    log.debug('ancestor %s was not setup', ancestor)
                    continue
                if ancestor in factory.was_torndown:
                    log.debug('ancestor %s already torn down', ancestor)
                    continue
                setup = factory.was_setup[ancestor]
                log.debug("%s setup ancestor %s", setup, ancestor)
                if setup is self:
                    self.teardownContext(ancestor)
        else:
            self.teardownContext(context)

    def teardownContext(self, context):
        log.debug("%s teardown context %s", self, context)
        if self.factory:
            if context in self.factory.was_torndown:
                return
            self.factory.was_torndown[context] = self
        if isclass(context):
            names = self.classTeardown
        else:
            names = self.moduleTeardown
            if hasattr(context, '__path__'):
                names = self.packageTeardown + names
        try_run(context, names)
        self.config.plugins.stopContext(context)

    # FIXME the wrapping has to move to the factory?
    def _get_wrapped_tests(self):
        for test in self._get_tests():
            if isinstance(test, Test) or isinstance(test, unittest.TestSuite):
                yield test
            else:
                yield Test(test,
                           config=self.config,
                           resultProxy=self.resultProxy)

    _tests = property(_get_wrapped_tests, LazySuite._set_tests, None,
                      "Access the tests in this suite. Tests are returned "
                      "inside of a context wrapper.")

class ContextSuiteFactory(object):
    """Factory for ContextSuites. Called with a collection of tests,
    the factory decides on a hierarchy of contexts by introspecting
    the collection or the tests themselves to find the objects
    containing the test objects. It always returns one suite, but that
    suite may consist of a hierarchy of nested suites.
    """
    suiteClass = ContextSuite
    def __init__(self, config=None, suiteClass=None, resultProxy=_def):
        if config is None:
            config = Config()
        self.config = config
        if suiteClass is not None:
            self.suiteClass = suiteClass
        # Using a singleton to represent default instead of None allows
        # passing resultProxy=None to turn proxying off.
        if resultProxy is _def:
            resultProxy = ResultProxyFactory(config=config)
        self.resultProxy = resultProxy
        self.suites = {}
        self.context = {}
        self.was_setup = {}
        self.was_torndown = {}

    def __call__(self, tests, **kw):
        """Return ``ContextSuite`` for tests. ``tests`` may either
        be a callable (in which case the resulting ContextSuite will
        have no parent context and be evaluated lazily) or an
        iterable. In that case the tests will wrapped in
        nose.case.Test, be examined and the context of each found and a
        suite of suites returned, organized into a stack with the
        outermost suites belonging to the outermost contexts.
        """
        log.debug("Create suite for %s", tests)
        context = kw.pop('context', getattr(tests, 'context', None))
        log.debug("tests %s context %s", tests, context)
        if context is None:
            tests = self.wrapTests(tests)
            try:
                context = self.findContext(tests)
            except MixedContextError:
                return self.makeSuite(self.mixedSuites(tests), None, **kw)
        return self.makeSuite(tests, context, **kw)

    def ancestry(self, context):
        """Return the ancestry of the context (that is, all of the
        packages and modules containing the context), in order of
        descent with the outermost ancestor last.
        This method is a generator.
        """
        log.debug("get ancestry %s", context)
        if context is None:
            return
        # Methods include reference to module they are defined in, we
        # don't want that, instead want the module the class is in now
        # (classes are re-ancestored elsewhere).
        if hasattr(context, 'im_class'):
            context = context.im_class
        elif hasattr(context, '__self__'):
            context = context.__self__.__class__
        if hasattr(context, '__module__'):
            ancestors = context.__module__.split('.')
        elif hasattr(context, '__name__'):
            ancestors = context.__name__.split('.')[:-1]
        else:
            raise TypeError("%s has no ancestors?" % context)
        while ancestors:
            log.debug(" %s ancestors %s", context, ancestors)
            yield resolve_name('.'.join(ancestors))
            ancestors.pop()

    def findContext(self, tests):
        if callable(tests) or isinstance(tests, unittest.TestSuite):
            return None
        context = None
        for test in tests:
            # Don't look at suites for contexts, only tests
            ctx = getattr(test, 'context', None)
            if ctx is None:
                continue
            if context is None:
                context = ctx
            elif context != ctx:
                raise MixedContextError(
                    "Tests with different contexts in same suite! %s != %s"
                    % (context, ctx))
        return context

    def makeSuite(self, tests, context, **kw):
        suite = self.suiteClass(
            tests, context=context, config=self.config, factory=self,
            resultProxy=self.resultProxy, **kw)
        if context is not None:
            self.suites.setdefault(context, []).append(suite)
            self.context.setdefault(suite, []).append(context)
            log.debug("suite %s has context %s", suite,
                      getattr(context, '__name__', None))
            for ancestor in self.ancestry(context):
                self.suites.setdefault(ancestor, []).append(suite)
                self.context[suite].append(ancestor)
                log.debug("suite %s has ancestor %s", suite, ancestor.__name__)
        return suite

    def mixedSuites(self, tests):
        """The complex case where there are tests that don't all share
        the same context. Groups tests into suites with common ancestors,
        according to the following (essentially tail-recursive) procedure:

        Starting with the context of the first test, if it is not
        None, look for tests in the remaining tests that share that
        ancestor. If any are found, group into a suite with that
        ancestor as the context, and replace the current suite with
        that suite. Continue this process for each ancestor of the
        first test, until all ancestors have been processed. At this
        point if any tests remain, recurse with those tests as the
        input, returning a list of the common suite (which may be the
        suite or test we started with, if no common tests were found)
        plus the results of recursion.
        """
        if not tests:
            return []
        head = tests.pop(0)
        if not tests:
            return [head] # short circuit when none are left to combine
        suite = head # the common ancestry suite, so far
        tail = tests[:]
        context = getattr(head, 'context', None)
        if context is not None:
            ancestors = [context] + [a for a in self.ancestry(context)]
            for ancestor in ancestors:
                common = [suite] # tests with ancestor in common, so far
                remain = [] # tests that remain to be processed
                for test in tail:
                    found_common = False
                    test_ctx = getattr(test, 'context', None)
                    if test_ctx is None:
                        remain.append(test)
                        continue
                    if test_ctx is ancestor:
                        common.append(test)
                        continue
                    for test_ancestor in self.ancestry(test_ctx):
                        if test_ancestor is ancestor:
                            common.append(test)
                            found_common = True
                            break
                    if not found_common:
                        remain.append(test)
                if common:
                    suite = self.makeSuite(common, ancestor)
                tail = self.mixedSuites(remain)
        return [suite] + tail

    def wrapTests(self, tests):
        log.debug("wrap %s", tests)
        if callable(tests) or isinstance(tests, unittest.TestSuite):
            log.debug("I won't wrap")
            return tests
        wrapped = []
        for test in tests:
            log.debug("wrapping %s", test)
            if isinstance(test, Test) or isinstance(test, unittest.TestSuite):
                wrapped.append(test)
            elif isinstance(test, ContextList):
                wrapped.append(self.makeSuite(test, context=test.context))
            else:
                wrapped.append(
                    Test(test, config=self.config, resultProxy=self.resultProxy)
                    )
        return wrapped

class ContextList(object):
    """Not quite a suite -- a group of tests in a context. This is used
    to hint the ContextSuiteFactory about what context the tests
    belong to, in cases where it may be ambiguous or missing.
    """
    def __init__(self, tests, context=None):
        self.tests = tests
        self.context = context

    def __iter__(self):
        return iter(self.tests)

class FinalizingSuiteWrapper(unittest.TestSuite):
    """Wraps suite and calls final function after suite has
    executed. Used to call final functions in cases (like running in
    the standard test runner) where test running is not under nose's
    control.
    """
    def __init__(self, suite, finalize):
        self.suite = suite
        self.finalize = finalize

    def __call__(self, *arg, **kw):
        return self.run(*arg, **kw)

    # 2.7 compat
    def __iter__(self):
        return iter(self.suite)

    def run(self, *arg, **kw):
        try:
            return self.suite(*arg, **kw)
        finally:
            self.finalize(*arg, **kw)

# backwards compat -- sort of
class TestDir:
    def __init__(*arg, **kw):
        raise NotImplementedError(
            "TestDir is not usable with nose 0.10. The class is present "
            "in nose.suite for backwards compatibility purposes but it "
            "may not be used.")

class TestModule:
    def __init__(*arg, **kw):
        raise NotImplementedError(
            "TestModule is not usable with nose 0.10. The class is present "
            "in nose.suite for backwards compatibility purposes but it "
            "may not be used.")


"""
This plugin bypasses the actual execution of tests, and instead just collects
test names. Fixtures are also bypassed, so running nosetests with the
collection plugin enabled should be very quick.

This plugin is useful in combination with the testid plugin (``--with-id``).
Run both together to get an indexed list of all tests, which will enable you to
run individual tests by index number.

This plugin is also useful for counting tests in a test suite, and making
people watching your demo think all of your tests pass.
"""
from nose.plugins.base import Plugin
from nose.case import Test
import logging
import unittest

log = logging.getLogger(__name__)

class CollectOnly(Plugin):
    """
    Collect and output test names only, don't run any tests.
    """
    name = "collect-only"
    enableOpt = 'collect_only'

    def options(self, parser, env):
        """Register commandline options.
        """
        parser.add_option('--collect-only',
                          action='store_true',
                          dest=self.enableOpt,
                          default=env.get('NOSE_COLLECT_ONLY'),
                          help="Enable collect-only: %s [COLLECT_ONLY]" %
                          (self.help()))

    def prepareTestLoader(self, loader):
        """Install collect-only suite class in TestLoader.
        """
        # Disable context awareness
        log.debug("Preparing test loader")
        loader.suiteClass = TestSuiteFactory(self.conf)

    def prepareTestCase(self, test):
        """Replace actual test with dummy that always passes.
        """
        # Return something that always passes
        log.debug("Preparing test case %s", test)
        if not isinstance(test, Test):
            return
        def run(result):
            # We need to make these plugin calls because there won't be
            # a result proxy, due to using a stripped-down test suite
            self.conf.plugins.startTest(test)
            result.startTest(test)
            self.conf.plugins.addSuccess(test)
            result.addSuccess(test)
            self.conf.plugins.stopTest(test)
            result.stopTest(test)
        return run

class TestSuiteFactory:
    """
    Factory for producing configured test suites.
    """
    def __init__(self, conf):
        self.conf = conf

    def __call__(self, tests=(), **kw):
        return TestSuite(tests, conf=self.conf)

class TestSuite(unittest.TestSuite):
    """
    Basic test suite that bypasses most proxy and plugin calls, but does
    wrap tests in a nose.case.Test so prepareTestCase will be called.
    """
    def __init__(self, tests=(), conf=None):
        self.conf = conf
        # Exec lazy suites: makes discovery depth-first
        if callable(tests):
            tests = tests()
        log.debug("TestSuite(%r)", tests)
        unittest.TestSuite.__init__(self, tests)

    def addTest(self, test):
        log.debug("Add test %s", test)
        if isinstance(test, unittest.TestSuite):
            self._tests.append(test)
        else:
            self._tests.append(Test(test, config=self.conf))


# -*- coding: utf-8 -*-
"""
Qt4's inputhook support function

Author: Christian Boos
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

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

from IPython.core.interactiveshell import InteractiveShell
from IPython.external.qt_for_kernel import QtCore, QtGui
from IPython.lib.inputhook import allow_CTRL_C, ignore_CTRL_C, stdin_ready

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------

def create_inputhook_qt4(mgr, app=None):
    """Create an input hook for running the Qt4 application event loop.

    Parameters
    ----------
    mgr : an InputHookManager

    app : Qt Application, optional.
        Running application to use.  If not given, we probe Qt for an
        existing application object, and create a new one if none is found.

    Returns
    -------
    A pair consisting of a Qt Application (either the one given or the
    one found or created) and a inputhook.

    Notes
    -----
    We use a custom input hook instead of PyQt4's default one, as it
    interacts better with the readline packages (issue #481).

    The inputhook function works in tandem with a 'pre_prompt_hook'
    which automatically restores the hook as an inputhook in case the
    latter has been temporarily disabled after having intercepted a
    KeyboardInterrupt.
    """

    if app is None:
        app = QtCore.QCoreApplication.instance()
        if app is None:
            app = QtGui.QApplication([" "])

    # Re-use previously created inputhook if any
    ip = InteractiveShell.instance()
    if hasattr(ip, '_inputhook_qt4'):
        return app, ip._inputhook_qt4

    # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of
    # hooks (they both share the got_kbdint flag)

    got_kbdint = [False]

    def inputhook_qt4():
        """PyOS_InputHook python hook for Qt4.

        Process pending Qt events and if there's no pending keyboard
        input, spend a short slice of time (50ms) running the Qt event
        loop.

        As a Python ctypes callback can't raise an exception, we catch
        the KeyboardInterrupt and temporarily deactivate the hook,
        which will let a *second* CTRL+C be processed normally and go
        back to a clean prompt line.
        """
        try:
            allow_CTRL_C()
            app = QtCore.QCoreApplication.instance()
            if not app: # shouldn't happen, but safer if it happens anyway...
                return 0
            app.processEvents(QtCore.QEventLoop.AllEvents, 300)
            if not stdin_ready():
                timer = QtCore.QTimer()
                timer.timeout.connect(app.quit)
                while not stdin_ready():
                    timer.start(50)
                    app.exec_()
                    timer.stop()
        except KeyboardInterrupt:
            ignore_CTRL_C()
            got_kbdint[0] = True
            print("\nKeyboardInterrupt - Ctrl-C again for new prompt")
            mgr.clear_inputhook()
        except: # NO exceptions are allowed to escape from a ctypes callback
            ignore_CTRL_C()
            from traceback import print_exc
            print_exc()
            print("Got exception from inputhook_qt4, unregistering.")
            mgr.clear_inputhook()
        finally:
            allow_CTRL_C()
        return 0

    def preprompthook_qt4(ishell):
        """'pre_prompt_hook' used to restore the Qt4 input hook

        (in case the latter was temporarily deactivated after a
        CTRL+C)
        """
        if got_kbdint[0]:
            mgr.set_inputhook(inputhook_qt4)
        got_kbdint[0] = False

    ip._inputhook_qt4 = inputhook_qt4
    ip.set_hook('pre_prompt_hook', preprompthook_qt4)

    return app, inputhook_qt4


# -*- coding: utf-8 -*-
import sys
import re
import inspect
import threading

if sys.version_info.major == 3:
    import urllib
    import urllib.parse
elif sys.version_info.major == 2:
    import urlparse
else:
    raise ImportError('Python version not supported.')

class Mapper(object):
    __instances = dict()

    _name = None

    _lock = None
    _data_store = None

    def __init__(self):
        """Create a new mapper instance.
           Using `Mapper.get()`_ is the prefered way.
        """
        self._lock = threading.RLock()
        self._data_store = list()

    @property
    def name(self):
        """Name of this instance."""
        return self._name

    @classmethod
    def get(cls, name=__name__):
        """Return a Mapper instance with the given name.
           If the name already exist return its instance.

           Does not work if a Mapper was created via its constructor.

           Using `Mapper.get()`_ is the prefered way.

        Args:
            name (str, optional): Name for the newly created instance.
                Defaults to `__name__`.

        Returns:
            Mapper: A mapper instance for the given name.

        Raises:
            TypeError: If a invalid name was given.
        """
        if not isinstance(name, str):
            raise TypeError('A mapper name must be a string')

        if name not in cls.__instances:
            cls.__instances[name] = cls()
            cls.__instances[name]._name = name

        return cls.__instances[name]

    def url(self, pattern, method=None, type_cast=None):
        """Decorator for registering a path pattern.

        Args:
            pattern (str): Regex pattern to match a certain path
            method (str, optional): Usually used to define one of GET, POST,
                PUT, DELETE. You may use whatever fits your situation though.
                Defaults to None.
            type_cast (dict, optional): Mapping between the param name and
                one of `int`, `float` or `bool`. The value reflected by the
                provided param name will than be casted to the given type.
                Defaults to None.
        """
        if not type_cast:
            type_cast = {}

        def decorator(function):
            self.add(pattern, function, method, type_cast)
            return function

        return decorator

    def s_url(self, path, method=None, type_cast=None):
        """Decorator for registering a simple path.

        Args:
            path (str): Path to be matched.
            method (str, optional): Usually used to define one of GET, POST,
                PUT, DELETE. You may use whatever fits your situation though.
                Defaults to None.
            type_cast (dict, optional): Mapping between the param name and
                one of `int`, `float` or `bool`. The value reflected by the
                provided param name will than be casted to the given type.
                Defaults to None.
        """
        if not type_cast:
            type_cast = {}

        def decorator(function):
            self.s_add(path, function, method, type_cast)
            return function

        return decorator

    def add(self, pattern, function, method=None, type_cast=None):
        """Function for registering a path pattern.

        Args:
            pattern (str): Regex pattern to match a certain path.
            function (function): Function to associate with this path.
            method (str, optional): Usually used to define one of GET, POST,
                PUT, DELETE. You may use whatever fits your situation though.
                Defaults to None.
            type_cast (dict, optional): Mapping between the param name and
                one of `int`, `float` or `bool`. The value reflected by the
                provided param name will than be casted to the given type.
                Defaults to None.
        """
        if not type_cast:
            type_cast = {}

        with self._lock:
            self._data_store.append({
                'pattern': pattern,
                'function': function,
                'method': method,
                'type_cast': type_cast,
            })

    def s_add(self, path, function, method=None, type_cast=None):
        """Function for registering a simple path.

        Args:
            path (str): Path to be matched.
            function (function): Function to associate with this path.
            method (str, optional): Usually used to define one of GET, POST,
                PUT, DELETE. You may use whatever fits your situation though.
                Defaults to None.
            type_cast (dict, optional): Mapping between the param name and
                one of `int`, `float` or `bool`. The value reflected by the
                provided param name will than be casted to the given type.
                Defaults to None.
        """
        with self._lock:
            try:
                path = '^/{}'.format(path.lstrip('/'))
                path = '{}/$'.format(path.rstrip('/'))
                path = path.replace('<', '(?P<')
                path = path.replace('>', '>[^/]*)')

                self.add(path, function, method, type_cast)
            except Exception:
                pass

    def clear(self):
        """Clears all data associated with the mappers data store"""
        with self._lock:
            try:
                del self._data_store[:]
            except Exception:
                pass

    def call(self, url, method=None, args=None):
        """Calls the first function matching the urls pattern and method.

        Args:
            url (str): Url for which to call a matching function.
            method (str, optional): The method used while registering a
                function.
                Defaults to None
            args (dict, optional): Additional args to be passed to the
                matching function.

        Returns:
            The functions return value or `None` if no function was called.
        """
        if not args:
            args = {}

        if sys.version_info.major == 3:
            data = urllib.parse.urlparse(url)
            path = data.path.rstrip('/') + '/'
            _args = dict(urllib.parse.parse_qs(data.query,
                                               keep_blank_values=True))
        elif sys.version_info.major == 2:
            data = urlparse.urlparse(url)
            path = data.path.rstrip('/') + '/'
            _args = dict(urlparse.parse_qs(data.query,
                                           keep_blank_values=True))

        for elem in self._data_store:
            pattern = elem['pattern']
            function = elem['function']
            _method = elem['method']
            type_cast = elem['type_cast']

            result = re.match(pattern, path)

            # Found matching method
            if result and _method == method:
                _args = dict(_args, **result.groupdict())

                # Unpack value lists (due to urllib.parse.parse_qs) in case
                # theres only one value available
                for key, val in _args.items():
                    if isinstance(_args[key], list) and len(_args[key]) == 1:
                        _args[key] = _args[key][0]

                # Apply typ-casting if necessary
                for key, val in type_cast.items():

                    # Not within available _args, no type-cast required
                    if key not in _args:
                        continue

                    # Is None or empty, no type-cast required
                    if not _args[key]:
                        continue

                    # Try and cast the values
                    if isinstance(_args[key], list):
                        for i, _val in enumerate(_args[key]):
                            _args[key][i] = self._cast(_val, val)
                    else:
                        _args[key] = self._cast(_args[key], val)

                requiered_args = self._get_function_args(function)
                for key, val in args.items():
                    if key in requiered_args:
                        _args[key] = val

                return function(**_args)

        return None

    def _cast(self, value, to):
        val = None

        if to == int:
            val = int(value)

        elif to == float:
            val = float(value)

        elif to == bool:
            if value.lower() in ('true', '1'):
                val = True

            elif value.lower() in ('false', '0'):
                val = False

        return val

    def _get_function_args(self, func):
        if sys.version_info.major == 3:
            sig = inspect.signature(func)
            args = list(sig.parameters)

        elif sys.version_info.major == 2:
            args, varargs, varkw, defaults = inspect.getargspec(func)

        return args


# System library imports
from IPython.external.qt import QtGui

# Local imports
from IPython.utils.traitlets import Bool
from console_widget import ConsoleWidget

class HistoryConsoleWidget(ConsoleWidget):
    """ A ConsoleWidget that keeps a history of the commands that have been
        executed and provides a readline-esque interface to this history.
    """

    #------ Configuration ------------------------------------------------------

    # If enabled, the input buffer will become "locked" to history movement when
    # an edit is made to a multi-line input buffer. To override the lock, use
    # Shift in conjunction with the standard history cycling keys.
    history_lock = Bool(False, config=True)

    #---------------------------------------------------------------------------
    # 'object' interface
    #---------------------------------------------------------------------------

    def __init__(self, *args, **kw):
        super(HistoryConsoleWidget, self).__init__(*args, **kw)

        # HistoryConsoleWidget protected variables.
        self._history = []
        self._history_edits = {}
        self._history_index = 0
        self._history_prefix = ''

    #---------------------------------------------------------------------------
    # 'ConsoleWidget' public interface
    #---------------------------------------------------------------------------

    def execute(self, source=None, hidden=False, interactive=False):
        """ Reimplemented to the store history.
        """
        if not hidden:
            history = self.input_buffer if source is None else source

        executed = super(HistoryConsoleWidget, self).execute(
            source, hidden, interactive)

        if executed and not hidden:
            # Save the command unless it was an empty string or was identical
            # to the previous command.
            history = history.rstrip()
            if history and (not self._history or self._history[-1] != history):
                self._history.append(history)

            # Emulate readline: reset all history edits.
            self._history_edits = {}

            # Move the history index to the most recent item.
            self._history_index = len(self._history)

        return executed

    #---------------------------------------------------------------------------
    # 'ConsoleWidget' abstract interface
    #---------------------------------------------------------------------------

    def _up_pressed(self, shift_modifier):
        """ Called when the up key is pressed. Returns whether to continue
            processing the event.
        """
        prompt_cursor = self._get_prompt_cursor()
        if self._get_cursor().blockNumber() == prompt_cursor.blockNumber():
            # Bail out if we're locked.
            if self._history_locked() and not shift_modifier:
                return False

            # Set a search prefix based on the cursor position.
            col = self._get_input_buffer_cursor_column()
            input_buffer = self.input_buffer
            if self._history_index == len(self._history) or \
                    (self._history_prefix and col != len(self._history_prefix)):
                self._history_index = len(self._history)
                self._history_prefix = input_buffer[:col]

            # Perform the search.
            self.history_previous(self._history_prefix,
                                    as_prefix=not shift_modifier)

            # Go to the first line of the prompt for seemless history scrolling.
            # Emulate readline: keep the cursor position fixed for a prefix
            # search.
            cursor = self._get_prompt_cursor()
            if self._history_prefix:
                cursor.movePosition(QtGui.QTextCursor.Right,
                                    n=len(self._history_prefix))
            else:
                cursor.movePosition(QtGui.QTextCursor.EndOfLine)
            self._set_cursor(cursor)

            return False

        return True

    def _down_pressed(self, shift_modifier):
        """ Called when the down key is pressed. Returns whether to continue
            processing the event.
        """
        end_cursor = self._get_end_cursor()
        if self._get_cursor().blockNumber() == end_cursor.blockNumber():
            # Bail out if we're locked.
            if self._history_locked() and not shift_modifier:
                return False

            # Perform the search.
            replaced = self.history_next(self._history_prefix,
                                            as_prefix=not shift_modifier)

            # Emulate readline: keep the cursor position fixed for a prefix
            # search. (We don't need to move the cursor to the end of the buffer
            # in the other case because this happens automatically when the
            # input buffer is set.)
            if self._history_prefix and replaced:
                cursor = self._get_prompt_cursor()
                cursor.movePosition(QtGui.QTextCursor.Right,
                                    n=len(self._history_prefix))
                self._set_cursor(cursor)

            return False

        return True

    #---------------------------------------------------------------------------
    # 'HistoryConsoleWidget' public interface
    #---------------------------------------------------------------------------

    def history_previous(self, substring='', as_prefix=True):
        """ If possible, set the input buffer to a previous history item.

        Parameters:
        -----------
        substring : str, optional
            If specified, search for an item with this substring.
        as_prefix : bool, optional
            If True, the substring must match at the beginning (default).

        Returns:
        --------
        Whether the input buffer was changed.
        """
        index = self._history_index
        replace = False
        while index > 0:
            index -= 1
            history = self._get_edited_history(index)
            if (as_prefix and history.startswith(substring)) \
                or (not as_prefix and substring in history):
                replace = True
                break

        if replace:
            self._store_edits()
            self._history_index = index
            self.input_buffer = history

        return replace

    def history_next(self, substring='', as_prefix=True):
        """ If possible, set the input buffer to a subsequent history item.

        Parameters:
        -----------
        substring : str, optional
            If specified, search for an item with this substring.
        as_prefix : bool, optional
            If True, the substring must match at the beginning (default).

        Returns:
        --------
        Whether the input buffer was changed.
        """
        index = self._history_index
        replace = False
        while self._history_index < len(self._history):
            index += 1
            history = self._get_edited_history(index)
            if (as_prefix and history.startswith(substring)) \
                or (not as_prefix and substring in history):
                replace = True
                break

        if replace:
            self._store_edits()
            self._history_index = index
            self.input_buffer = history

        return replace

    def history_tail(self, n=10):
        """ Get the local history list.

        Parameters:
        -----------
        n : int
            The (maximum) number of history items to get.
        """
        return self._history[-n:]

    def _request_update_session_history_length(self):
        msg_id = self.kernel_manager.shell_channel.execute('',
            silent=True,
            user_expressions={
                'hlen':'len(get_ipython().history_manager.input_hist_raw)',
                }
            )
        self._request_info['execute'][msg_id] = self._ExecutionRequest(msg_id, 'save_magic')

    def _handle_execute_reply(self, msg):
        """ Handles replies for code execution, here only session history length
        """
        msg_id = msg['parent_header']['msg_id']
        info = self._request_info['execute'].pop(msg_id,None)
        if info and info.kind == 'save_magic' and not self._hidden:
            content = msg['content']
            status = content['status']
            if status == 'ok':
                self._max_session_history=(int(content['user_expressions']['hlen']))

    def save_magic(self):
        # update the session history length
        self._request_update_session_history_length()

        file_name,extFilter = QtGui.QFileDialog.getSaveFileName(self,
            "Enter A filename",
            filter='Python File (*.py);; All files (*.*)'
            )

        # let's the user search/type for a file name, while the history length
        # is fetched

        if file_name:
            hist_range, ok = QtGui.QInputDialog.getText(self,
                'Please enter an interval of command to save',
                'Saving commands:',
                text=str('1-'+str(self._max_session_history))
                )
            if ok:
                self.execute("%save"+" "+file_name+" "+str(hist_range))

    #---------------------------------------------------------------------------
    # 'HistoryConsoleWidget' protected interface
    #---------------------------------------------------------------------------

    def _history_locked(self):
        """ Returns whether history movement is locked.
        """
        return (self.history_lock and
                (self._get_edited_history(self._history_index) !=
                 self.input_buffer) and
                (self._get_prompt_cursor().blockNumber() !=
                 self._get_end_cursor().blockNumber()))

    def _get_edited_history(self, index):
        """ Retrieves a history item, possibly with temporary edits.
        """
        if index in self._history_edits:
            return self._history_edits[index]
        elif index == len(self._history):
            return unicode()
        return self._history[index]

    def _set_history(self, history):
        """ Replace the current history with a sequence of history items.
        """
        self._history = list(history)
        self._history_edits = {}
        self._history_index = len(self._history)

    def _store_edits(self):
        """ If there are edits to the current input buffer, store them.
        """
        current = self.input_buffer
        if self._history_index == len(self._history) or \
                self._history[self._history_index] != current:
            self._history_edits[self._history_index] = current


import ply.lex as lex
import ply.yacc as yacc

from pprint import pprint

reserved = [
    'SELECT', 'FROM', 'WHERE', 'LIMIT', 'AND', 'OR', 'ORDER', 'BY', 'NOT'
]

tokens = (
    *reserved,
    'NAME', 'NUMBER', 'STRING',
    'EQUALS', 'LPAREN', 'RPAREN', 'STAR', 'COMMA'
)

t_STAR = r'\*'
t_COMMA = r','

def t_NAME(t):
    r'[A-Za-z_][A-Za-z0-9_]*'
    # to simplify lexing, we match identifiers and keywords as a single thing
    # if it's a keyword, we change the type to the name of that keyword
    if t.value.upper() in reserved:
        t.type = t.value.upper()
        t.value = t.value.upper()
    return t

def t_NUMBER(t):
    r'\d+'
    t.value = int(t.value)
    return t

def t_STRING(t):
    r"'([^'\\]+|\\'|\\\\)*'"
    t.value = t.value.replace(r'\\', chr(92)).replace(r"\'", r"'")[1:-1]
    return t

t_EQUALS = r'='
t_LPAREN = r'\('
t_RPAREN = r'\)'

t_ignore = " \t\n"

def t_newline(t):
    r'\n+'
    t.lexer.lineno += t.value.count("\n")

def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

lex.lex()

precedence = (
    ('left', 'OR'),
    ('left', 'AND'),
    ('left', 'NOT'),
    ('left', 'EQUALS'),
)

def p_empty(p):
    'empty :'
    pass

def p_statement_select(p):
    'statement : select postpositions'
    p[0] = {**p[1], **p[2]}

def p_postpositions(p):
    '''
    postpositions : LIMIT NUMBER postpositions
                  | ORDER BY colspec postpositions
                  | empty
    '''
    if len(p) > 2:
        if p[1] == "LIMIT":
            postposition = {
                "limit": p[2]
            }
            rest = p[3] if p[3] else {}
        elif p[1:3] == ["ORDER", "BY"]:
            postposition = {
                "order by": p[3]
            }
            rest = p[4] if p[4] else {}
        else:
            breakpoint()
        p[0] = {**postposition, **rest}
    else:
        p[0] = {}

def p_select(p):
    'select : SELECT colspec FROM value condition'
    p[0] = {
        "type": "select",
        "columns": p[2],
        "table": p[4],
    }
    if p[5]:
        p[0]["condition"] = p[5]

def p_colspec(p):
    '''
    colspec : STAR
            | NAME
            | function
            | NAME COMMA colspec
            | function COMMA colspec
    '''
    rest = p[3] if len(p) > 3 else []
    if p[1] == "*":
        p[0] = [{"type": "star"}]
    elif isinstance(p[1], dict) and p[1].get("type") == "function":
        p[0] = [p[1], *rest]
    elif p[1]:
        p[0] = [
            {
                "type": "name",
                "value": p[1],
            },
            *rest,
        ]
    else:
        p[0] = []

def p_condition(p):
    '''
    condition : WHERE expression
              | empty
    '''
    if len(p) > 2:
        p[0] = p[2]
    else:
        p[0] = None

def p_function(p):
    'function : NAME LPAREN value RPAREN'
    p[0] = {
        "type": "function",
        "value": {
            "name": p[1],
            "arg": p[3],
        },
    }

def p_expression(p):
    '''
    expression : value
               | expression AND expression
               | expression OR expression
               | expression EQUALS expression
               | NOT expression
               | LPAREN expression RPAREN
    '''
    if len(p) < 3:
        p[0] = p[1]
    elif len(p) == 3:  # not
        p[0] = {
            "op": "not",
            "args": [p[2]],
        }
    elif p[1] == "(":
        p[0] = p[2]
    else:
        p[0] = {
            "op": p[2].lower(),
            "args": [p[1], p[3]],
        }

def p_value(p):
    '''
    value : NUMBER
          | STRING
          | NAME
    '''
    p[0] = {
        "type": "name" if p.slice[1].type == "NAME" else "literal",
        "value": p[1],
    }

def p_error(p):
    raise SyntaxError("Invalid mojo-sql statement")

yacc.yacc(start="statement")


#!/usr/bin/env python
"""Example integrating an IPython kernel into a GUI App.

This trivial GUI application internally starts an IPython kernel, to which Qt
consoles can be connected either by the user at the command line or started
from the GUI itself, via a button.  The GUI can also manipulate one variable in
the kernel's namespace, and print the namespace to the console.

Play with it by running the script and then opening one or more consoles, and
pushing the 'Counter++' and 'Namespace' buttons.

Upon exit, it should automatically close all consoles opened from the GUI.

Consoles attached separately from a terminal will not be terminated, though
they will notice that their kernel died.

Ref: Modified from wxPython source code wxPython/samples/simple/simple.py
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import sys

import wx

from internal_ipkernel import InternalIPKernel

#-----------------------------------------------------------------------------
# Functions and classes
#-----------------------------------------------------------------------------

class MyFrame(wx.Frame, InternalIPKernel):
    """
    This is MyFrame.  It just shows a few controls on a wxPanel,
    and has a simple menu.
    """

    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title,
                          pos=(150, 150), size=(350, 285))

        # Create the menubar
        menuBar = wx.MenuBar()

        # and a menu
        menu = wx.Menu()

        # add an item to the menu, using \tKeyName automatically
        # creates an accelerator, the third param is some help text
        # that will show up in the statusbar
        menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")

        # bind the menu event to an event handler
        self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)

        # and put the menu on the menubar
        menuBar.Append(menu, "&File")
        self.SetMenuBar(menuBar)

        self.CreateStatusBar()

        # Now create the Panel to put the other controls on.
        panel = wx.Panel(self)

        # and a few controls
        text = wx.StaticText(panel, -1, "Hello World!")
        text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
        text.SetSize(text.GetBestSize())
        qtconsole_btn = wx.Button(panel, -1, "Qt Console")
        ns_btn = wx.Button(panel, -1, "Namespace")
        count_btn = wx.Button(panel, -1, "Count++")
        close_btn = wx.Button(panel, -1, "Quit")

        # bind the button events to handlers
        self.Bind(wx.EVT_BUTTON, self.new_qt_console, qtconsole_btn)
        self.Bind(wx.EVT_BUTTON, self.print_namespace, ns_btn)
        self.Bind(wx.EVT_BUTTON, self.count, count_btn)
        self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, close_btn)

        # Use a sizer to layout the controls, stacked vertically and with
        # a 10 pixel border around each
        sizer = wx.BoxSizer(wx.VERTICAL)
        for ctrl in [text, qtconsole_btn, ns_btn, count_btn, close_btn]:
            sizer.Add(ctrl, 0, wx.ALL, 10)
        panel.SetSizer(sizer)
        panel.Layout()

        # Start the IPython kernel with gui support
        self.init_ipkernel('wx')

    def OnTimeToClose(self, evt):
        """Event handler for the button click."""
        print("See ya later!")
        sys.stdout.flush()
        self.cleanup_consoles(evt)
        self.Close()
        # Not sure why, but our IPython kernel seems to prevent normal WX
        # shutdown, so an explicit exit() call is needed.
        sys.exit()

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, "Simple wxPython App")
        self.SetTopWindow(frame)
        frame.Show(True)
        self.ipkernel = frame.ipkernel
        return True

#-----------------------------------------------------------------------------
# Main script
#-----------------------------------------------------------------------------

if __name__ == '__main__':
    app = MyApp(redirect=False, clearSigInt=False)

    # Very important, IPython-specific step: this gets GUI event loop
    # integration going, and it replaces calling app.MainLoop()
    app.ipkernel.start()


#!/usr/bin/env python
""" A script/util to upgrade all files in a directory

This is rather conservative in its approach, only copying/overwriting
new and unedited files.

To be used by "upgrade" feature.
"""
try:
    from IPython.external.path import path
except ImportError:
    from path import path

import hashlib, pickle

def showdiff(old,new):
    import difflib
    d = difflib.Differ()
    lines = d.compare(old.lines(),new.lines())
    realdiff = False
    for l in lines:
        print l,
        if not realdiff and not l[0].isspace():
            realdiff = True
    return realdiff

def upgrade_dir(srcdir, tgtdir):
    """ Copy over all files in srcdir to tgtdir w/ native line endings

    Creates .upgrade_report in tgtdir that stores md5sums of all files
    to notice changed files b/w upgrades.
    """

    def pr(s):
        print s
    junk = ['.svn','ipythonrc*','*.pyc', '*.pyo', '*~', '.hg']

    def ignorable(p):
        for pat in junk:
            if p.startswith(pat) or p.fnmatch(pat):
                return True
        return False

    modded = []
    files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()]
    #print files
    rep = tgtdir / '.upgrade_report'
    try:
        rpt = pickle.load(rep.open())
    except:
        rpt = {}

    for f in files:
        if ignorable(f):
            continue
        src = srcdir / f
        tgt = tgtdir / f
        if not tgt.isfile():
            pr("Creating %s" % str(tgt))

            tgt.write_text(src.text())
            rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
        else:
            cont = tgt.text()
            sum = rpt.get(str(tgt), None)
            #print sum
            if sum and hashlib.md5(cont).hexdigest() == sum:
                pr("%s: Unedited, installing new version" % tgt)
                tgt.write_text(src.text())
                rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
            else:
                pr(' == Modified, skipping %s, diffs below == ' % tgt)
                #rpt[str(tgt)] = hashlib.md5(tgt.bytes()).hexdigest()
                real = showdiff(tgt,src)
                pr('') # empty line
                if not real:
                    pr("(Ok, it was identical, only upgrading checksum)")
                    rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
                else:
                    modded.append(tgt)

        #print rpt
    pickle.dump(rpt, rep.open('w'))
    if modded:
        print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:"
        for m in modded:
            print m

import sys
if __name__ == "__main__":
    upgrade_dir(path(sys.argv[1]), path(sys.argv[2]))