<commit_before>"""Tests for the object inspection functionality.
"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from inspect import Signature, Parameter
import os
import re
import sys

import nose.tools as nt

from .. import oinspect
from IPython.core.magic import (Magics, magics_class, line_magic,
                                cell_magic, line_cell_magic,
                                register_line_magic, register_cell_magic,
                                register_line_cell_magic)
from decorator import decorator
from IPython import get_ipython
from IPython.testing.decorators import skipif
from IPython.testing.tools import AssertPrints, AssertNotPrints
from IPython.utils.path import compress_user
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

inspector = oinspect.Inspector()
ip = get_ipython()

#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------

# WARNING: since this test checks the line number where a function is
# defined, if any code is inserted above, the following line will need to be
# updated.  Do NOT insert any whitespace between the next line and the function
# definition below.
THIS_LINE_NUMBER = 43  # Put here the actual number of this line

from unittest import TestCase

class Test(TestCase):

    def test_find_source_lines(self):
        self.assertEqual(oinspect.find_source_lines(Test.test_find_source_lines),
                    THIS_LINE_NUMBER+6)

# A couple of utilities to ensure these tests work the same from a source or a
# binary install
def pyfile(fname):
    return os.path.normcase(re.sub('.py[co]$', '.py', fname))

def match_pyfiles(f1, f2):
    nt.assert_equal(pyfile(f1), pyfile(f2))

def test_find_file():
    match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))

def test_find_file_decorated1():

    @decorator
    def noop1(f):
        def wrapper(*a, **kw):
            return f(*a, **kw)
        return wrapper

    @noop1
    def f(x):
        "My docstring"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring")

def test_find_file_decorated2():

    @decorator
    def noop2(f, *a, **kw):
        return f(*a, **kw)

    @noop2
    @noop2
    @noop2
    def f(x):
        "My docstring 2"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring 2")

def test_find_file_magic():
    run = ip.find_line_magic('run')
    nt.assert_not_equal(oinspect.find_file(run), None)

# A few generic objects we can then inspect in the tests below

class Call(object):
    """This is the class docstring."""

    def __init__(self, x, y=1):
        """This is the constructor docstring."""

    def __call__(self, *a, **kw):
        """This is the call docstring."""

    def method(self, x, z=2):
        """Some method's docstring"""

class HasSignature(object):
    """This is the class docstring."""
    __signature__ = Signature([Parameter('test', Parameter.POSITIONAL_OR_KEYWORD)])

    def __init__(self, *args):
        """This is the init docstring"""

class SimpleClass(object):
    def method(self, x, z=2):
        """Some method's docstring"""

class OldStyle:
    """An old-style class for testing."""
    pass

def f(x, y=2, *a, **kw):
    """A simple function."""

def g(y, z=3, *a, **kw):
    pass  # no docstring

@register_line_magic
def lmagic(line):
    "A line magic"

@register_cell_magic
def cmagic(line, cell):
    "A cell magic"

@register_line_cell_magic
def lcmagic(line, cell=None):
    "A line/cell magic"

@magics_class
class SimpleMagics(Magics):
    @line_magic
    def Clmagic(self, cline):
        "A class-based line magic"

    @cell_magic
    def Ccmagic(self, cline, ccell):
        "A class-based cell magic"

    @line_cell_magic
    def Clcmagic(self, cline, ccell=None):
        "A class-based line/cell magic"

class Awkward(object):
    def __getattr__(self, name):
        raise Exception(name)

class NoBoolCall:
    """
    callable with `__bool__` raising should still be inspect-able.
    """

    def __call__(self):
        """does nothing"""
        pass

    def __bool__(self):
        """just raise NotImplemented"""
        raise NotImplementedError('Must be implemented')

class SerialLiar(object):
    """Attribute accesses always get another copy of the same class.

    unittest.mock.call does something similar, but it's not ideal for testing
    as the failure mode is to eat all your RAM. This gives up after 10k levels.
    """
    def __init__(self, max_fibbing_twig, lies_told=0):
        if lies_told > 10000:
            raise RuntimeError('Nose too long, honesty is the best policy')
        self.max_fibbing_twig = max_fibbing_twig
        self.lies_told = lies_told
        max_fibbing_twig[0] = max(max_fibbing_twig[0], lies_told)

    def __getattr__(self, item):
        return SerialLiar(self.max_fibbing_twig, self.lies_told + 1)

#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------

def test_info():
    "Check that Inspector.info fills out various fields as expected."
    i = inspector.info(Call, oname='Call')
    nt.assert_equal(i['type_name'], 'type')
    expted_class = str(type(type))  # <class 'type'> (Python 3) or <type 'type'>
    nt.assert_equal(i['base_class'], expted_class)
    nt.assert_regex(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'( at 0x[0-9a-f]{1,9})?>")
    fname = __file__
    if fname.endswith(".pyc"):
        fname = fname[:-1]
    # case-insensitive comparison needed on some filesystems
    # e.g. Windows:
    nt.assert_equal(i['file'].lower(), compress_user(fname).lower())
    nt.assert_equal(i['definition'], None)
    nt.assert_equal(i['docstring'], Call.__doc__)
    nt.assert_equal(i['source'], None)
    nt.assert_true(i['isclass'])
    nt.assert_equal(i['init_definition'], "Call(x, y=1)")
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)

    i = inspector.info(Call, detail_level=1)
    nt.assert_not_equal(i['source'], None)
    nt.assert_equal(i['docstring'], None)

    c = Call(1)
    c.__doc__ = "Modified instance docstring"
    i = inspector.info(c)
    nt.assert_equal(i['type_name'], 'Call')
    nt.assert_equal(i['docstring'], "Modified instance docstring")
    nt.assert_equal(i['class_docstring'], Call.__doc__)
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
    nt.assert_equal(i['call_docstring'], Call.__call__.__doc__)

    # Test old-style classes, which for example may not have an __init__ method.
    if not py3compat.PY3:
        i = inspector.info(OldStyle)
        nt.assert_equal(i['type_name'], 'classobj')

        i = inspector.info(OldStyle())
        nt.assert_equal(i['type_name'], 'instance')
        nt.assert_equal(i['docstring'], OldStyle.__doc__)

def test_class_signature():
    info = inspector.info(HasSignature, 'HasSignature')
    nt.assert_equal(info['init_definition'], "HasSignature(test)")
    nt.assert_equal(info['init_docstring'], HasSignature.__init__.__doc__)

def test_info_awkward():
    # Just test that this doesn't throw an error.
    inspector.info(Awkward())

def test_bool_raise():
    inspector.info(NoBoolCall())

def test_info_serialliar():
    fib_tracker = [0]
    inspector.info(SerialLiar(fib_tracker))

    # Nested attribute access should be cut off at 100 levels deep to avoid
    # infinite loops: https://github.com/ipython/ipython/issues/9122
    nt.assert_less(fib_tracker[0], 9000)

def test_calldef_none():
    # We should ignore __call__ for all of these.
    for obj in [f, SimpleClass().method, any, str.upper]:
        print(obj)
        i = inspector.info(obj)
        nt.assert_is(i['call_def'], None)

def f_kwarg(pos, *, kwonly):
    pass

def test_definition_kwonlyargs():
    i = inspector.info(f_kwarg, oname='f_kwarg')  # analysis:ignore
    nt.assert_equal(i['definition'], "f_kwarg(pos, *, kwonly)")

def test_getdoc():
    class A(object):
        """standard docstring"""
        pass

    class B(object):
        """standard docstring"""
        def getdoc(self):
            return "custom docstring"

    class C(object):
        """standard docstring"""
        def getdoc(self):
            return None

    a = A()
    b = B()
    c = C()

    nt.assert_equal(oinspect.getdoc(a), "standard docstring")
    nt.assert_equal(oinspect.getdoc(b), "custom docstring")
    nt.assert_equal(oinspect.getdoc(c), "standard docstring")

def test_empty_property_has_no_source():
    i = inspector.info(property(), detail_level=1)
    nt.assert_is(i['source'], None)

def test_property_sources():
    import zlib

    class A(object):
        @property
        def foo(self):
            return 'bar'

        foo = foo.setter(lambda self, v: setattr(self, 'bar', v))

        id = property(id)
        compress = property(zlib.compress)

    i = inspector.info(A.foo, detail_level=1)
    nt.assert_in('def foo(self):', i['source'])
    nt.assert_in('lambda self, v:', i['source'])

    i = inspector.info(A.id, detail_level=1)
    nt.assert_in('fget = <function id>', i['source'])

    i = inspector.info(A.compress, detail_level=1)
    nt.assert_in('fget = <function zlib.compress>', i['source'])

def test_property_docstring_is_in_info_for_detail_level_0():
    class A(object):
        @property
        def foobar(self):
            """This is `foobar` property."""
            pass

    ip.user_ns['a_obj'] = A()
    nt.assert_equal(
        'This is `foobar` property.',
        ip.object_inspect('a_obj.foobar', detail_level=0)['docstring'])

    ip.user_ns['a_cls'] = A
    nt.assert_equal(
        'This is `foobar` property.',
        ip.object_inspect('a_cls.foobar', detail_level=0)['docstring'])

def test_pdef():
    # See gh-1914
    def foo(): pass
    inspector.pdef(foo, 'foo')

def test_pinfo_nonascii():
    # See gh-1177
    from . import nonascii2
    ip.user_ns['nonascii2'] = nonascii2
    ip._inspect('pinfo', 'nonascii2', detail_level=1)

def test_pinfo_docstring_no_source():
    """Docstring should be included with detail_level=1 if there is no source"""
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'str.format', detail_level=0)
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'str.format', detail_level=1)

def test_pinfo_no_docstring_if_source():
    """Docstring should not be included with detail_level=1 if source is found"""
    def foo():
        """foo has a docstring"""

    ip.user_ns['foo'] = foo

    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'foo', detail_level=0)
    with AssertPrints('Source:'):
        ip._inspect('pinfo', 'foo', detail_level=1)
    with AssertNotPrints('Docstring:'):
        ip._inspect('pinfo', 'foo', detail_level=1)

def test_pinfo_magic():
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'lsmagic', detail_level=0)

    with AssertPrints('Source:'):
        ip._inspect('pinfo', 'lsmagic', detail_level=1)

def test_init_colors():
    # ensure colors are not present in signature info
    info = inspector.info(HasSignature)
    init_def = info['init_definition']
    nt.assert_not_in('[0m', init_def)

def test_builtin_init():
    info = inspector.info(list)
    init_def = info['init_definition']
    # Python < 3.4 can't get init definition from builtins,
    # but still exercise the inspection in case of error-raising bugs.
    if sys.version_info >= (3,4):
        nt.assert_is_not_none(init_def)

<commit_msg>remove dead code<commit_after>"""Tests for the object inspection functionality.
"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from inspect import Signature, Parameter
import os
import re
import sys

import nose.tools as nt

from .. import oinspect
from IPython.core.magic import (Magics, magics_class, line_magic,
                                cell_magic, line_cell_magic,
                                register_line_magic, register_cell_magic,
                                register_line_cell_magic)
from decorator import decorator
from IPython import get_ipython
from IPython.testing.decorators import skipif
from IPython.testing.tools import AssertPrints, AssertNotPrints
from IPython.utils.path import compress_user
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

inspector = oinspect.Inspector()
ip = get_ipython()

#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------

# WARNING: since this test checks the line number where a function is
# defined, if any code is inserted above, the following line will need to be
# updated.  Do NOT insert any whitespace between the next line and the function
# definition below.
THIS_LINE_NUMBER = 43  # Put here the actual number of this line

from unittest import TestCase

class Test(TestCase):

    def test_find_source_lines(self):
        self.assertEqual(oinspect.find_source_lines(Test.test_find_source_lines),
                    THIS_LINE_NUMBER+6)

# A couple of utilities to ensure these tests work the same from a source or a
# binary install
def pyfile(fname):
    return os.path.normcase(re.sub('.py[co]$', '.py', fname))

def match_pyfiles(f1, f2):
    nt.assert_equal(pyfile(f1), pyfile(f2))

def test_find_file():
    match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))

def test_find_file_decorated1():

    @decorator
    def noop1(f):
        def wrapper(*a, **kw):
            return f(*a, **kw)
        return wrapper

    @noop1
    def f(x):
        "My docstring"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring")

def test_find_file_decorated2():

    @decorator
    def noop2(f, *a, **kw):
        return f(*a, **kw)

    @noop2
    @noop2
    @noop2
    def f(x):
        "My docstring 2"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring 2")

def test_find_file_magic():
    run = ip.find_line_magic('run')
    nt.assert_not_equal(oinspect.find_file(run), None)

# A few generic objects we can then inspect in the tests below

class Call(object):
    """This is the class docstring."""

    def __init__(self, x, y=1):
        """This is the constructor docstring."""

    def __call__(self, *a, **kw):
        """This is the call docstring."""

    def method(self, x, z=2):
        """Some method's docstring"""

class HasSignature(object):
    """This is the class docstring."""
    __signature__ = Signature([Parameter('test', Parameter.POSITIONAL_OR_KEYWORD)])

    def __init__(self, *args):
        """This is the init docstring"""

class SimpleClass(object):
    def method(self, x, z=2):
        """Some method's docstring"""

class OldStyle:
    """An old-style class for testing."""
    pass

def f(x, y=2, *a, **kw):
    """A simple function."""

def g(y, z=3, *a, **kw):
    pass  # no docstring

@register_line_magic
def lmagic(line):
    "A line magic"

@register_cell_magic
def cmagic(line, cell):
    "A cell magic"

@register_line_cell_magic
def lcmagic(line, cell=None):
    "A line/cell magic"

@magics_class
class SimpleMagics(Magics):
    @line_magic
    def Clmagic(self, cline):
        "A class-based line magic"

    @cell_magic
    def Ccmagic(self, cline, ccell):
        "A class-based cell magic"

    @line_cell_magic
    def Clcmagic(self, cline, ccell=None):
        "A class-based line/cell magic"

class Awkward(object):
    def __getattr__(self, name):
        raise Exception(name)

class NoBoolCall:
    """
    callable with `__bool__` raising should still be inspect-able.
    """

    def __call__(self):
        """does nothing"""
        pass

    def __bool__(self):
        """just raise NotImplemented"""
        raise NotImplementedError('Must be implemented')

class SerialLiar(object):
    """Attribute accesses always get another copy of the same class.

    unittest.mock.call does something similar, but it's not ideal for testing
    as the failure mode is to eat all your RAM. This gives up after 10k levels.
    """
    def __init__(self, max_fibbing_twig, lies_told=0):
        if lies_told > 10000:
            raise RuntimeError('Nose too long, honesty is the best policy')
        self.max_fibbing_twig = max_fibbing_twig
        self.lies_told = lies_told
        max_fibbing_twig[0] = max(max_fibbing_twig[0], lies_told)

    def __getattr__(self, item):
        return SerialLiar(self.max_fibbing_twig, self.lies_told + 1)

#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------

def test_info():
    "Check that Inspector.info fills out various fields as expected."
    i = inspector.info(Call, oname='Call')
    nt.assert_equal(i['type_name'], 'type')
    expted_class = str(type(type))  # <class 'type'> (Python 3) or <type 'type'>
    nt.assert_equal(i['base_class'], expted_class)
    nt.assert_regex(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'( at 0x[0-9a-f]{1,9})?>")
    fname = __file__
    if fname.endswith(".pyc"):
        fname = fname[:-1]
    # case-insensitive comparison needed on some filesystems
    # e.g. Windows:
    nt.assert_equal(i['file'].lower(), compress_user(fname).lower())
    nt.assert_equal(i['definition'], None)
    nt.assert_equal(i['docstring'], Call.__doc__)
    nt.assert_equal(i['source'], None)
    nt.assert_true(i['isclass'])
    nt.assert_equal(i['init_definition'], "Call(x, y=1)")
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)

    i = inspector.info(Call, detail_level=1)
    nt.assert_not_equal(i['source'], None)
    nt.assert_equal(i['docstring'], None)

    c = Call(1)
    c.__doc__ = "Modified instance docstring"
    i = inspector.info(c)
    nt.assert_equal(i['type_name'], 'Call')
    nt.assert_equal(i['docstring'], "Modified instance docstring")
    nt.assert_equal(i['class_docstring'], Call.__doc__)
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
    nt.assert_equal(i['call_docstring'], Call.__call__.__doc__)

def test_class_signature():
    info = inspector.info(HasSignature, 'HasSignature')
    nt.assert_equal(info['init_definition'], "HasSignature(test)")
    nt.assert_equal(info['init_docstring'], HasSignature.__init__.__doc__)

def test_info_awkward():
    # Just test that this doesn't throw an error.
    inspector.info(Awkward())

def test_bool_raise():
    inspector.info(NoBoolCall())

def test_info_serialliar():
    fib_tracker = [0]
    inspector.info(SerialLiar(fib_tracker))

    # Nested attribute access should be cut off at 100 levels deep to avoid
    # infinite loops: https://github.com/ipython/ipython/issues/9122
    nt.assert_less(fib_tracker[0], 9000)

def test_calldef_none():
    # We should ignore __call__ for all of these.
    for obj in [f, SimpleClass().method, any, str.upper]:
        print(obj)
        i = inspector.info(obj)
        nt.assert_is(i['call_def'], None)

def f_kwarg(pos, *, kwonly):
    pass

def test_definition_kwonlyargs():
    i = inspector.info(f_kwarg, oname='f_kwarg')  # analysis:ignore
    nt.assert_equal(i['definition'], "f_kwarg(pos, *, kwonly)")

def test_getdoc():
    class A(object):
        """standard docstring"""
        pass

    class B(object):
        """standard docstring"""
        def getdoc(self):
            return "custom docstring"

    class C(object):
        """standard docstring"""
        def getdoc(self):
            return None

    a = A()
    b = B()
    c = C()

    nt.assert_equal(oinspect.getdoc(a), "standard docstring")
    nt.assert_equal(oinspect.getdoc(b), "custom docstring")
    nt.assert_equal(oinspect.getdoc(c), "standard docstring")

def test_empty_property_has_no_source():
    i = inspector.info(property(), detail_level=1)
    nt.assert_is(i['source'], None)

def test_property_sources():
    import zlib

    class A(object):
        @property
        def foo(self):
            return 'bar'

        foo = foo.setter(lambda self, v: setattr(self, 'bar', v))

        id = property(id)
        compress = property(zlib.compress)

    i = inspector.info(A.foo, detail_level=1)
    nt.assert_in('def foo(self):', i['source'])
    nt.assert_in('lambda self, v:', i['source'])

    i = inspector.info(A.id, detail_level=1)
    nt.assert_in('fget = <function id>', i['source'])

    i = inspector.info(A.compress, detail_level=1)
    nt.assert_in('fget = <function zlib.compress>', i['source'])

def test_property_docstring_is_in_info_for_detail_level_0():
    class A(object):
        @property
        def foobar(self):
            """This is `foobar` property."""
            pass

    ip.user_ns['a_obj'] = A()
    nt.assert_equal(
        'This is `foobar` property.',
        ip.object_inspect('a_obj.foobar', detail_level=0)['docstring'])

    ip.user_ns['a_cls'] = A
    nt.assert_equal(
        'This is `foobar` property.',
        ip.object_inspect('a_cls.foobar', detail_level=0)['docstring'])

def test_pdef():
    # See gh-1914
    def foo(): pass
    inspector.pdef(foo, 'foo')

def test_pinfo_nonascii():
    # See gh-1177
    from . import nonascii2
    ip.user_ns['nonascii2'] = nonascii2
    ip._inspect('pinfo', 'nonascii2', detail_level=1)

def test_pinfo_docstring_no_source():
    """Docstring should be included with detail_level=1 if there is no source"""
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'str.format', detail_level=0)
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'str.format', detail_level=1)

def test_pinfo_no_docstring_if_source():
    """Docstring should not be included with detail_level=1 if source is found"""
    def foo():
        """foo has a docstring"""

    ip.user_ns['foo'] = foo

    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'foo', detail_level=0)
    with AssertPrints('Source:'):
        ip._inspect('pinfo', 'foo', detail_level=1)
    with AssertNotPrints('Docstring:'):
        ip._inspect('pinfo', 'foo', detail_level=1)

def test_pinfo_magic():
    with AssertPrints('Docstring:'):
        ip._inspect('pinfo', 'lsmagic', detail_level=0)

    with AssertPrints('Source:'):
        ip._inspect('pinfo', 'lsmagic', detail_level=1)

def test_init_colors():
    # ensure colors are not present in signature info
    info = inspector.info(HasSignature)
    init_def = info['init_definition']
    nt.assert_not_in('[0m', init_def)

def test_builtin_init():
    info = inspector.info(list)
    init_def = info['init_definition']
    # Python < 3.4 can't get init definition from builtins,
    # but still exercise the inspection in case of error-raising bugs.
    if sys.version_info >= (3,4):
        nt.assert_is_not_none(init_def)

<|endoftext|>


<commit_before>"""Tests for the object inspection functionality.
"""
#-----------------------------------------------------------------------------
#  Copyright (C) 2010-2011 The IPython Development Team.
#
#  Distributed under the terms of the BSD License.
#
#  The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function

# Stdlib imports
import os
import re

# Third-party imports
import nose.tools as nt

# Our own imports
from .. import oinspect
from IPython.core.magic import (Magics, magics_class, line_magic,
                                cell_magic, line_cell_magic,
                                register_line_magic, register_cell_magic,
                                register_line_cell_magic)
from IPython.external.decorator import decorator
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

inspector = oinspect.Inspector()
ip = get_ipython()

#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------

# WARNING: since this test checks the line number where a function is
# defined, if any code is inserted above, the following line will need to be
# updated.  Do NOT insert any whitespace between the next line and the function
# definition below.
THIS_LINE_NUMBER = 48  # Put here the actual number of this line
def test_find_source_lines():
    nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
                    THIS_LINE_NUMBER+1)

# A couple of utilities to ensure these tests work the same from a source or a
# binary install
def pyfile(fname):
    return re.sub('.py[co]$', '.py', fname)

def match_pyfiles(f1, f2):
    nt.assert_equal(pyfile(f1), pyfile(f2))

def test_find_file():
    match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))

def test_find_file_decorated1():

    @decorator
    def noop1(f):
        def wrapper():
            return f(*a, **kw)
        return wrapper

    @noop1
    def f(x):
        "My docstring"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring")

def test_find_file_decorated2():

    @decorator
    def noop2(f, *a, **kw):
        return f(*a, **kw)

    @noop2
    def f(x):
        "My docstring 2"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring 2")

def test_find_file_magic():
    run = ip.find_line_magic('run')
    nt.assert_not_equal(oinspect.find_file(run), None)

# A few generic objects we can then inspect in the tests below

class Call(object):
    """This is the class docstring."""

    def __init__(self, x, y=1):
        """This is the constructor docstring."""

    def __call__(self, *a, **kw):
        """This is the call docstring."""

    def method(self, x, z=2):
        """Some method's docstring"""

class OldStyle:
    """An old-style class for testing."""
    pass

def f(x, y=2, *a, **kw):
    """A simple function."""

def g(y, z=3, *a, **kw):
    pass  # no docstring

@register_line_magic
def lmagic(line):
    "A line magic"

@register_cell_magic
def cmagic(line, cell):
    "A cell magic"

@register_line_cell_magic
def lcmagic(line, cell=None):
    "A line/cell magic"

@magics_class
class SimpleMagics(Magics):
    @line_magic
    def Clmagic(self, cline):
        "A class-based line magic"

    @cell_magic
    def Ccmagic(self, cline, ccell):
        "A class-based cell magic"

    @line_cell_magic
    def Clcmagic(self, cline, ccell=None):
        "A class-based line/cell magic"

def check_calltip(obj, name, call, docstring):
    """Generic check pattern all calltip tests will use"""
    info = inspector.info(obj, name)
    call_line, ds = oinspect.call_tip(info)
    nt.assert_equal(call_line, call)
    nt.assert_equal(ds, docstring)

#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------

def test_calltip_class():
    check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)

def test_calltip_instance():
    c = Call(1)
    check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)

def test_calltip_method():
    c = Call(1)
    check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)

def test_calltip_function():
    check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)

def test_calltip_function2():
    check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')

def test_calltip_builtin():
    check_calltip(sum, 'sum', None, sum.__doc__)

def test_calltip_line_magic():
    check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")

def test_calltip_cell_magic():
    check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")

def test_calltip_line_magic():
    check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
                  "A line/cell magic")

def test_class_magics():
    cm = SimpleMagics(ip)
    ip.register_magics(cm)
    check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
                  "A class-based line magic")
    check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
                  "A class-based cell magic")
    check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
                  "A class-based line/cell magic")

def test_info():
    "Check that Inspector.info fills out various fields as expected."
    i = inspector.info(Call, oname='Call')
    nt.assert_equal(i['type_name'], 'type')
    expted_class = str(type(type))  # <class 'type'> (Python 3) or <type 'type'>
    nt.assert_equal(i['base_class'], expted_class)
    nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
    fname = __file__
    if fname.endswith(".pyc"):
        fname = fname[:-1]
    # case-insensitive comparison needed on some filesystems
    # e.g. Windows:
    nt.assert_equal(i['file'].lower(), fname.lower())
    nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
    nt.assert_equal(i['docstring'], Call.__doc__)
    nt.assert_equal(i['source'], None)
    nt.assert_true(i['isclass'])
    nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)

    i = inspector.info(Call, detail_level=1)
    nt.assert_not_equal(i['source'], None)
    nt.assert_equal(i['docstring'], None)

    c = Call(1)
    c.__doc__ = "Modified instance docstring"
    i = inspector.info(c)
    nt.assert_equal(i['type_name'], 'Call')
    nt.assert_equal(i['docstring'], "Modified instance docstring")
    nt.assert_equal(i['class_docstring'], Call.__doc__)
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
    nt.assert_equal(i['call_docstring'], c.__call__.__doc__)

    # Test old-style classes, which for example may not have an __init__ method.
    if not py3compat.PY3:
        i = inspector.info(OldStyle)
        nt.assert_equal(i['type_name'], 'classobj')

        i = inspector.info(OldStyle())
        nt.assert_equal(i['type_name'], 'instance')
        nt.assert_equal(i['docstring'], OldStyle.__doc__)

def test_getdoc():
    class A(object):
        """standard docstring"""
        pass

    class B(object):
        """standard docstring"""
        def getdoc(self):
            return "custom docstring"

    class C(object):
        """standard docstring"""
        def getdoc(self):
            return None

    a = A()
    b = B()
    c = C()

    nt.assert_equal(oinspect.getdoc(a), "standard docstring")
    nt.assert_equal(oinspect.getdoc(b), "custom docstring")
    nt.assert_equal(oinspect.getdoc(c), "standard docstring")
<commit_msg>Add failing test for gh-1914<commit_after>"""Tests for the object inspection functionality.
"""
#-----------------------------------------------------------------------------
#  Copyright (C) 2010-2011 The IPython Development Team.
#
#  Distributed under the terms of the BSD License.
#
#  The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function

# Stdlib imports
import os
import re

# Third-party imports
import nose.tools as nt

# Our own imports
from .. import oinspect
from IPython.core.magic import (Magics, magics_class, line_magic,
                                cell_magic, line_cell_magic,
                                register_line_magic, register_cell_magic,
                                register_line_cell_magic)
from IPython.external.decorator import decorator
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

inspector = oinspect.Inspector()
ip = get_ipython()

#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------

# WARNING: since this test checks the line number where a function is
# defined, if any code is inserted above, the following line will need to be
# updated.  Do NOT insert any whitespace between the next line and the function
# definition below.
THIS_LINE_NUMBER = 48  # Put here the actual number of this line
def test_find_source_lines():
    nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
                    THIS_LINE_NUMBER+1)

# A couple of utilities to ensure these tests work the same from a source or a
# binary install
def pyfile(fname):
    return re.sub('.py[co]$', '.py', fname)

def match_pyfiles(f1, f2):
    nt.assert_equal(pyfile(f1), pyfile(f2))

def test_find_file():
    match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))

def test_find_file_decorated1():

    @decorator
    def noop1(f):
        def wrapper():
            return f(*a, **kw)
        return wrapper

    @noop1
    def f(x):
        "My docstring"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring")

def test_find_file_decorated2():

    @decorator
    def noop2(f, *a, **kw):
        return f(*a, **kw)

    @noop2
    def f(x):
        "My docstring 2"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring 2")

def test_find_file_magic():
    run = ip.find_line_magic('run')
    nt.assert_not_equal(oinspect.find_file(run), None)

# A few generic objects we can then inspect in the tests below

class Call(object):
    """This is the class docstring."""

    def __init__(self, x, y=1):
        """This is the constructor docstring."""

    def __call__(self, *a, **kw):
        """This is the call docstring."""

    def method(self, x, z=2):
        """Some method's docstring"""

class OldStyle:
    """An old-style class for testing."""
    pass

def f(x, y=2, *a, **kw):
    """A simple function."""

def g(y, z=3, *a, **kw):
    pass  # no docstring

@register_line_magic
def lmagic(line):
    "A line magic"

@register_cell_magic
def cmagic(line, cell):
    "A cell magic"

@register_line_cell_magic
def lcmagic(line, cell=None):
    "A line/cell magic"

@magics_class
class SimpleMagics(Magics):
    @line_magic
    def Clmagic(self, cline):
        "A class-based line magic"

    @cell_magic
    def Ccmagic(self, cline, ccell):
        "A class-based cell magic"

    @line_cell_magic
    def Clcmagic(self, cline, ccell=None):
        "A class-based line/cell magic"

def check_calltip(obj, name, call, docstring):
    """Generic check pattern all calltip tests will use"""
    info = inspector.info(obj, name)
    call_line, ds = oinspect.call_tip(info)
    nt.assert_equal(call_line, call)
    nt.assert_equal(ds, docstring)

#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------

def test_calltip_class():
    check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)

def test_calltip_instance():
    c = Call(1)
    check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)

def test_calltip_method():
    c = Call(1)
    check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)

def test_calltip_function():
    check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)

def test_calltip_function2():
    check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')

def test_calltip_builtin():
    check_calltip(sum, 'sum', None, sum.__doc__)

def test_calltip_line_magic():
    check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")

def test_calltip_cell_magic():
    check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")

def test_calltip_line_magic():
    check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
                  "A line/cell magic")

def test_class_magics():
    cm = SimpleMagics(ip)
    ip.register_magics(cm)
    check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
                  "A class-based line magic")
    check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
                  "A class-based cell magic")
    check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
                  "A class-based line/cell magic")

def test_info():
    "Check that Inspector.info fills out various fields as expected."
    i = inspector.info(Call, oname='Call')
    nt.assert_equal(i['type_name'], 'type')
    expted_class = str(type(type))  # <class 'type'> (Python 3) or <type 'type'>
    nt.assert_equal(i['base_class'], expted_class)
    nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
    fname = __file__
    if fname.endswith(".pyc"):
        fname = fname[:-1]
    # case-insensitive comparison needed on some filesystems
    # e.g. Windows:
    nt.assert_equal(i['file'].lower(), fname.lower())
    nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
    nt.assert_equal(i['docstring'], Call.__doc__)
    nt.assert_equal(i['source'], None)
    nt.assert_true(i['isclass'])
    nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)

    i = inspector.info(Call, detail_level=1)
    nt.assert_not_equal(i['source'], None)
    nt.assert_equal(i['docstring'], None)

    c = Call(1)
    c.__doc__ = "Modified instance docstring"
    i = inspector.info(c)
    nt.assert_equal(i['type_name'], 'Call')
    nt.assert_equal(i['docstring'], "Modified instance docstring")
    nt.assert_equal(i['class_docstring'], Call.__doc__)
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
    nt.assert_equal(i['call_docstring'], c.__call__.__doc__)

    # Test old-style classes, which for example may not have an __init__ method.
    if not py3compat.PY3:
        i = inspector.info(OldStyle)
        nt.assert_equal(i['type_name'], 'classobj')

        i = inspector.info(OldStyle())
        nt.assert_equal(i['type_name'], 'instance')
        nt.assert_equal(i['docstring'], OldStyle.__doc__)

def test_getdoc():
    class A(object):
        """standard docstring"""
        pass

    class B(object):
        """standard docstring"""
        def getdoc(self):
            return "custom docstring"

    class C(object):
        """standard docstring"""
        def getdoc(self):
            return None

    a = A()
    b = B()
    c = C()

    nt.assert_equal(oinspect.getdoc(a), "standard docstring")
    nt.assert_equal(oinspect.getdoc(b), "custom docstring")
    nt.assert_equal(oinspect.getdoc(c), "standard docstring")

def test_pdef():
    # See gh-1914
    def foo(): pass
    inspector.pdef(foo, 'foo')
<|endoftext|>


<commit_before>"""Tests for the object inspection functionality.
"""
#-----------------------------------------------------------------------------
#  Copyright (C) 2010-2011 The IPython Development Team.
#
#  Distributed under the terms of the BSD License.
#
#  The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function

# Stdlib imports
import os
import re

# Third-party imports
import nose.tools as nt

# Our own imports
from .. import oinspect
from IPython.core.magic import (Magics, magics_class, line_magic,
                                cell_magic, line_cell_magic,
                                register_line_magic, register_cell_magic,
                                register_line_cell_magic)
from IPython.external.decorator import decorator
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

inspector = oinspect.Inspector()
ip = get_ipython()

#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------

# WARNING: since this test checks the line number where a function is
# defined, if any code is inserted above, the following line will need to be
# updated.  Do NOT insert any whitespace between the next line and the function
# definition below.
THIS_LINE_NUMBER = 48  # Put here the actual number of this line
def test_find_source_lines():
    nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
                    THIS_LINE_NUMBER+1)

# A couple of utilities to ensure these tests work the same from a source or a
# binary install
def pyfile(fname):
    return re.sub('.py[co]$', '.py', fname)

def match_pyfiles(f1, f2):
    nt.assert_equal(pyfile(f1), pyfile(f2))

def test_find_file():
    match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))

def test_find_file_decorated1():

    @decorator
    def noop1(f):
        def wrapper():
            return f(*a, **kw)
        return wrapper

    @noop1
    def f(x):
        "My docstring"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring")

def test_find_file_decorated2():

    @decorator
    def noop2(f, *a, **kw):
        return f(*a, **kw)

    @noop2
    def f(x):
        "My docstring 2"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring 2")

def test_find_file_magic():
    run = ip.find_line_magic('run')
    nt.assert_not_equal(oinspect.find_file(run), None)

# A few generic objects we can then inspect in the tests below

class Call(object):
    """This is the class docstring."""

    def __init__(self, x, y=1):
        """This is the constructor docstring."""

    def __call__(self, *a, **kw):
        """This is the call docstring."""

    def method(self, x, z=2):
        """Some method's docstring"""

class OldStyle:
    """An old-style class for testing."""
    pass

def f(x, y=2, *a, **kw):
    """A simple function."""

def g(y, z=3, *a, **kw):
    pass  # no docstring

@register_line_magic
def lmagic(line):
    "A line magic"

@register_cell_magic
def cmagic(line, cell):
    "A cell magic"

@register_line_cell_magic
def lcmagic(line, cell=None):
    "A line/cell magic"

@magics_class
class SimpleMagics(Magics):
    @line_magic
    def Clmagic(self, cline):
        "A class-based line magic"

    @cell_magic
    def Ccmagic(self, cline, ccell):
        "A class-based cell magic"

    @line_cell_magic
    def Clcmagic(self, cline, ccell=None):
        "A class-based line/cell magic"

def check_calltip(obj, name, call, docstring):
    """Generic check pattern all calltip tests will use"""
    info = inspector.info(obj, name)
    call_line, ds = oinspect.call_tip(info)
    nt.assert_equal(call_line, call)
    nt.assert_equal(ds, docstring)

#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------

def test_calltip_class():
    check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)

def test_calltip_instance():
    c = Call(1)
    check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)

def test_calltip_method():
    c = Call(1)
    check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)

def test_calltip_function():
    check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)

def test_calltip_function2():
    check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')

def test_calltip_builtin():
    check_calltip(sum, 'sum', None, sum.__doc__)

def test_calltip_line_magic():
    check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")

def test_calltip_cell_magic():
    check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")

def test_calltip_line_magic():
    check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
                  "A line/cell magic")

def test_class_magics():
    cm = SimpleMagics(ip)
    ip.register_magics(cm)
    check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
                  "A class-based line magic")
    check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
                  "A class-based cell magic")
    check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
                  "A class-based line/cell magic")

def test_info():
    "Check that Inspector.info fills out various fields as expected."
    i = inspector.info(Call, oname='Call')
    nt.assert_equal(i['type_name'], 'type')
    expted_class = str(type(type))  # <class 'type'> (Python 3) or <type 'type'>
    nt.assert_equal(i['base_class'], expted_class)
    nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
    fname = __file__
    if fname.endswith(".pyc"):
        fname = fname[:-1]
    # case-insensitive comparison needed on some filesystems
    # e.g. Windows:
    nt.assert_equal(i['file'].lower(), fname.lower())
    nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
    nt.assert_equal(i['docstring'], Call.__doc__)
    nt.assert_equal(i['source'], None)
    nt.assert_true(i['isclass'])
    nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)

    i = inspector.info(Call, detail_level=1)
    nt.assert_not_equal(i['source'], None)
    nt.assert_equal(i['docstring'], None)

    c = Call(1)
    c.__doc__ = "Modified instance docstring"
    i = inspector.info(c)
    nt.assert_equal(i['type_name'], 'Call')
    nt.assert_equal(i['docstring'], "Modified instance docstring")
    nt.assert_equal(i['class_docstring'], Call.__doc__)
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
    nt.assert_equal(i['call_docstring'], c.__call__.__doc__)

    # Test old-style classes, which for example may not have an __init__ method.
    if not py3compat.PY3:
        i = inspector.info(OldStyle)
        nt.assert_equal(i['type_name'], 'classobj')

        i = inspector.info(OldStyle())
        nt.assert_equal(i['type_name'], 'instance')
        nt.assert_equal(i['docstring'], OldStyle.__doc__)

def test_getdoc():
    class A(object):
        """standard docstring"""
        pass

    class B(object):
        """standard docstring"""
        def getdoc(self):
            return "custom docstring"

    class C(object):
        """standard docstring"""
        def getdoc(self):
            return None

    a = A()
    b = B()
    c = C()

    nt.assert_equal(oinspect.getdoc(a), "standard docstring")
    nt.assert_equal(oinspect.getdoc(b), "custom docstring")
    nt.assert_equal(oinspect.getdoc(c), "standard docstring")
<commit_msg>Fix for #1908, use os.path.normcase for safe filename comparisons<commit_after>"""Tests for the object inspection functionality.
"""
#-----------------------------------------------------------------------------
#  Copyright (C) 2010-2011 The IPython Development Team.
#
#  Distributed under the terms of the BSD License.
#
#  The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function

# Stdlib imports
import os
import re

# Third-party imports
import nose.tools as nt

# Our own imports
from .. import oinspect
from IPython.core.magic import (Magics, magics_class, line_magic,
                                cell_magic, line_cell_magic,
                                register_line_magic, register_cell_magic,
                                register_line_cell_magic)
from IPython.external.decorator import decorator
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

inspector = oinspect.Inspector()
ip = get_ipython()

#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------

# WARNING: since this test checks the line number where a function is
# defined, if any code is inserted above, the following line will need to be
# updated.  Do NOT insert any whitespace between the next line and the function
# definition below.
THIS_LINE_NUMBER = 48  # Put here the actual number of this line
def test_find_source_lines():
    nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
                    THIS_LINE_NUMBER+1)

# A couple of utilities to ensure these tests work the same from a source or a
# binary install
def pyfile(fname):
    return re.sub('.py[co]$', '.py', fname)

def match_pyfiles(f1, f2):
    nt.assert_equal(os.path.normcase(pyfile(f1)), os.path.normcase(pyfile(f2)))

def test_find_file():
    match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))

def test_find_file_decorated1():

    @decorator
    def noop1(f):
        def wrapper():
            return f(*a, **kw)
        return wrapper

    @noop1
    def f(x):
        "My docstring"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring")

def test_find_file_decorated2():

    @decorator
    def noop2(f, *a, **kw):
        return f(*a, **kw)

    @noop2
    def f(x):
        "My docstring 2"

    match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
    nt.assert_equal(f.__doc__, "My docstring 2")

def test_find_file_magic():
    run = ip.find_line_magic('run')
    nt.assert_not_equal(oinspect.find_file(run), None)

# A few generic objects we can then inspect in the tests below

class Call(object):
    """This is the class docstring."""

    def __init__(self, x, y=1):
        """This is the constructor docstring."""

    def __call__(self, *a, **kw):
        """This is the call docstring."""

    def method(self, x, z=2):
        """Some method's docstring"""

class OldStyle:
    """An old-style class for testing."""
    pass

def f(x, y=2, *a, **kw):
    """A simple function."""

def g(y, z=3, *a, **kw):
    pass  # no docstring

@register_line_magic
def lmagic(line):
    "A line magic"

@register_cell_magic
def cmagic(line, cell):
    "A cell magic"

@register_line_cell_magic
def lcmagic(line, cell=None):
    "A line/cell magic"

@magics_class
class SimpleMagics(Magics):
    @line_magic
    def Clmagic(self, cline):
        "A class-based line magic"

    @cell_magic
    def Ccmagic(self, cline, ccell):
        "A class-based cell magic"

    @line_cell_magic
    def Clcmagic(self, cline, ccell=None):
        "A class-based line/cell magic"

def check_calltip(obj, name, call, docstring):
    """Generic check pattern all calltip tests will use"""
    info = inspector.info(obj, name)
    call_line, ds = oinspect.call_tip(info)
    nt.assert_equal(call_line, call)
    nt.assert_equal(ds, docstring)

#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------

def test_calltip_class():
    check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)

def test_calltip_instance():
    c = Call(1)
    check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)

def test_calltip_method():
    c = Call(1)
    check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)

def test_calltip_function():
    check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)

def test_calltip_function2():
    check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')

def test_calltip_builtin():
    check_calltip(sum, 'sum', None, sum.__doc__)

def test_calltip_line_magic():
    check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")

def test_calltip_cell_magic():
    check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")

def test_calltip_line_magic():
    check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
                  "A line/cell magic")

def test_class_magics():
    cm = SimpleMagics(ip)
    ip.register_magics(cm)
    check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
                  "A class-based line magic")
    check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
                  "A class-based cell magic")
    check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
                  "A class-based line/cell magic")

def test_info():
    "Check that Inspector.info fills out various fields as expected."
    i = inspector.info(Call, oname='Call')
    nt.assert_equal(i['type_name'], 'type')
    expted_class = str(type(type))  # <class 'type'> (Python 3) or <type 'type'>
    nt.assert_equal(i['base_class'], expted_class)
    nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
    fname = __file__
    if fname.endswith(".pyc"):
        fname = fname[:-1]
    # case-insensitive comparison needed on some filesystems
    # e.g. Windows:
    nt.assert_equal(i['file'].lower(), fname.lower())
    nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
    nt.assert_equal(i['docstring'], Call.__doc__)
    nt.assert_equal(i['source'], None)
    nt.assert_true(i['isclass'])
    nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)

    i = inspector.info(Call, detail_level=1)
    nt.assert_not_equal(i['source'], None)
    nt.assert_equal(i['docstring'], None)

    c = Call(1)
    c.__doc__ = "Modified instance docstring"
    i = inspector.info(c)
    nt.assert_equal(i['type_name'], 'Call')
    nt.assert_equal(i['docstring'], "Modified instance docstring")
    nt.assert_equal(i['class_docstring'], Call.__doc__)
    nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
    nt.assert_equal(i['call_docstring'], c.__call__.__doc__)

    # Test old-style classes, which for example may not have an __init__ method.
    if not py3compat.PY3:
        i = inspector.info(OldStyle)
        nt.assert_equal(i['type_name'], 'classobj')

        i = inspector.info(OldStyle())
        nt.assert_equal(i['type_name'], 'instance')
        nt.assert_equal(i['docstring'], OldStyle.__doc__)

def test_getdoc():
    class A(object):
        """standard docstring"""
        pass

    class B(object):
        """standard docstring"""
        def getdoc(self):
            return "custom docstring"

    class C(object):
        """standard docstring"""
        def getdoc(self):
            return None

    a = A()
    b = B()
    c = C()

    nt.assert_equal(oinspect.getdoc(a), "standard docstring")
    nt.assert_equal(oinspect.getdoc(b), "custom docstring")
    nt.assert_equal(oinspect.getdoc(c), "standard docstring")
<|endoftext|>


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

import sys
import pdb
import time
from opcua import Client
from opcua.tools import endpoint_to_strings

uri = "opc.tcp://192.168.1.186:4840"

def get_children(node):
    descs = node.get_children_descriptions()
    descs.sort(key=lambda x: x.BrowseName)
    return descs

def get_endpoints(uri):
    client = Client(uri, timeout=2)
    endpoints = client.connect_and_get_server_endpoints()
    for i, endpoint in enumerate(endpoints, start=1):
        print('[INFO]', 'Endpoint %s:' % i)
        for record in endpoint_to_strings(endpoint):  # name, value
            print('  >>>   %s: %s' % record)
    return endpoints

pdb.set_trace()
# endpoints = get_endpoints(uri)

client = Client(uri)
client.connect()
print client.get_node("ns=4;i=21001").get_value()
client.disconnect()
sys.exit()

root = client.get_root_node()
pdb.set_trace()
# for line in root.get_children():
#     print(line)

# sensor =root.get_child([0])
# descriptions = get_children(root)
# print(descriptions)

sys.exit()
client = Client(uri)
client.connect()
pdb.set_trace()

print("client connect")
root = client.get_root_node()
print("root:", root)
objects = client.get_objects_node()
print(objects)
childnode = objects.get_child("5:Simulation")
print(childnode)
while True:
    counter = client.get_node("ns=5;s=Counter1")
    count = counter.get_value()
    print(count)
    time.sleep(1)

pdb.set_trace()
client.disconnect()
sys.exit()
#temp = client.get_node("ns=2;i=2")

modes = {
    '':  (
    'description',
    'name',
    'nodes',
    'product_uri',
    'max_chunkcount',
    'max_messagesize',
    ),

    '()': (
    'export_xml',
    'find_endpoint',
    'find_servers',
    'find_servers_on_network',
    'get_endpoints',
    'get_namespace_array',
    'get_namespace_index',
    'get_node',
    'get_objects_node',
    'get_root_node',
    'get_server_node',
    'get_values',
    'import_xml',oca
    )
    }

import pdb; pdb.set_trace()
for mode in modes:
    for call in modes[mode]:
        try:
            operation = eval('client.%s%s' % (
                call,
                mode,
                ))
            print('[INFO]', call, '>>>>>', operation)
        except:
            print('[ERROR]', sys.exc_info())

client.disconnect()
sys.exit()
# while True:
#           Temp = client.get_node("ns=2;i=2")
#           Temperature = Temp.get_value()
#           print(Temperature)

# '__class__',
# '__delattr__', '__dict__', '__doc__', '__enter__', '__exit__', '__format__',
# '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
# '__reduce__', '__reduce_ex__',
# '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
# '__weakref__', '_add_anonymous_auth', '_add_certificate_auth',
# '_add_user_auth', '_encrypt_password',
# '_password', '_policy_ids', '_server_nonce', '_session_counter',
# '_username',

# 'activate_session', 'application_uri', 'close_secure_channel',
# 'close_session',
# 'connect', 'connect_and_find_servers', 'connect_and_find_servers_on_network',
# 'connect_and_get_server_endpoints', 'connect_socket', 'create_session',
# 'create_subscription', 'delete_nodes',
# 'disconnect', 'disconnect_socket',
# 'keepalive',
# 'load_client_certificate', 'load_enums', 'load_private_key',
# 'open_secure_channel', 'reconciliate_subscription', 'register_namespace',
# 'register_nodes',
# 'secure_channel_id', 'secure_channel_timeout', 'security_policy',
# 'send_hello', 'server_policy_id', 'server_policy_uri', 'server_url',
# 'session_timeout', 'set_password', 'set_security', 'set_security_string',
# 'set_user', 'set_values', 'uaclient', 'unregister_nodes', 'user_certificate',
# 'user_private_key'
# 'load_type_definitions',

<commit_msg>generic bugfix (fast message)<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import pdb
import time
from opcua import Client
from opcua.tools import endpoint_to_strings

uri = "opc.tcp://192.168.1.186:4840"

def get_children(node):
    descs = node.get_children_descriptions()
    descs.sort(key=lambda x: x.BrowseName)
    return descs

def get_endpoints(uri):
    client = Client(uri, timeout=2)
    endpoints = client.connect_and_get_server_endpoints()
    for i, endpoint in enumerate(endpoints, start=1):
        print('[INFO]', 'Endpoint %s:' % i)
        for record in endpoint_to_strings(endpoint):  # name, value
            print('  >>>   %s: %s' % record)
    return endpoints

pdb.set_trace()
# endpoints = get_endpoints(uri)

client = Client(uri)
client.connect()
print client.get_node("ns=4;s=::AsGlobalPV:VersionPLC").get_value()
client.disconnect()
sys.exit()

root = client.get_root_node()
pdb.set_trace()
# for line in root.get_children():
#     print(line)

# sensor =root.get_child([0])
# descriptions = get_children(root)
# print(descriptions)

sys.exit()
client = Client(uri)
client.connect()
pdb.set_trace()

print("client connect")
root = client.get_root_node()
print("root:", root)
objects = client.get_objects_node()
print(objects)
childnode = objects.get_child("5:Simulation")
print(childnode)
while True:
    counter = client.get_node("ns=5;s=Counter1")
    count = counter.get_value()
    print(count)
    time.sleep(1)

pdb.set_trace()
client.disconnect()
sys.exit()
#temp = client.get_node("ns=2;i=2")

modes = {
    '':  (
    'description',
    'name',
    'nodes',
    'product_uri',
    'max_chunkcount',
    'max_messagesize',
    ),

    '()': (
    'export_xml',
    'find_endpoint',
    'find_servers',
    'find_servers_on_network',
    'get_endpoints',
    'get_namespace_array',
    'get_namespace_index',
    'get_node',
    'get_objects_node',
    'get_root_node',
    'get_server_node',
    'get_values',
    'import_xml',oca
    )
    }

import pdb; pdb.set_trace()
for mode in modes:
    for call in modes[mode]:
        try:
            operation = eval('client.%s%s' % (
                call,
                mode,
                ))
            print('[INFO]', call, '>>>>>', operation)
        except:
            print('[ERROR]', sys.exc_info())

client.disconnect()
sys.exit()
# while True:
#           Temp = client.get_node("ns=2;i=2")
#           Temperature = Temp.get_value()
#           print(Temperature)

# '__class__',
# '__delattr__', '__dict__', '__doc__', '__enter__', '__exit__', '__format__',
# '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
# '__reduce__', '__reduce_ex__',
# '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
# '__weakref__', '_add_anonymous_auth', '_add_certificate_auth',
# '_add_user_auth', '_encrypt_password',
# '_password', '_policy_ids', '_server_nonce', '_session_counter',
# '_username',

# 'activate_session', 'application_uri', 'close_secure_channel',
# 'close_session',
# 'connect', 'connect_and_find_servers', 'connect_and_find_servers_on_network',
# 'connect_and_get_server_endpoints', 'connect_socket', 'create_session',
# 'create_subscription', 'delete_nodes',
# 'disconnect', 'disconnect_socket',
# 'keepalive',
# 'load_client_certificate', 'load_enums', 'load_private_key',
# 'open_secure_channel', 'reconciliate_subscription', 'register_namespace',
# 'register_nodes',
# 'secure_channel_id', 'secure_channel_timeout', 'security_policy',
# 'send_hello', 'server_policy_id', 'server_policy_uri', 'server_url',
# 'session_timeout', 'set_password', 'set_security', 'set_security_string',
# 'set_user', 'set_values', 'uaclient', 'unregister_nodes', 'user_certificate',
# 'user_private_key'
# 'load_type_definitions',

<|endoftext|>


<commit_before>#!/usr/bin/python

#!/usr/bin/pyton

#    Copyright 2011 Lockheed Martin

'''
Created on August 19, 2011

@author: jpoyau
'''
import urllib2
import logging
from lr.lib.couch_change import *
from resource_data import appConfig, ResourceDataModel
from node import NodeModel
import threading
import multiprocessing
import ctypes
import pprint
import atexit
import json
from datetime import timedelta
from pylons import config
appConfig = config['app_conf']

log = logging.getLogger(__name__)

def _resourceDataPredicate(change, database):
    try:
      return "doc" in change and change["doc"]["doc_type"] == "resource_data"
    except:
        return False

def _distributableDataPredicate(change, database):
    try:
      return "doc" in change and change["doc"]["doc_type"] == "resource_data_distributable"
    except:
        return False

def _updateDatabaseViews(change, database):
    try:
        designDocs = database.view('_all_docs',include_docs=True,
                                                        startkey='_design%2F',endkey='_design0')
        for designDoc in designDocs:
            if designDoc.doc.has_key('views') and len(designDoc.doc['views']) > 0:
                viewName = "{0}/_view/{1}".format(designDoc.id,designDoc.doc['views'].keys()[0])
                log.debug('start view update %s' % viewName)
                log.debug(len(database.view(viewName)))
    except Exception as e:
        log.error(e)

def _doDistribute(change, database):

    log.debug('start distribute')
    data = json.dumps({"dist":"dist"})
    request = urllib2.Request(appConfig['lr.distribute.url'],data,{'Content-Type':'application/json; charset=utf-8'})
    log.debug(pprint.pformat(request))
    response = urllib2.urlopen(request)
    log.debug('end distribute')

class RecordDistributableChange(DatabaseChangeHandler):
    def _canHandle(self, change, database):
        return _distributableDataPredicate(change, database)

    def _getResourceDataDoc(self, docID, database):
        # There exist already a resource_data document for the distributable
        # get it and see if it needs to be updated.
        try:
            return  ResourceDataModel(database[docID])._specData
        except Exception as e:
            log.error("\n\nCannot find existing resource_data doc for distributable: \n{0}".format(
                            docID))
            log.exception(e)
            return None

    def _updateResourceData(self, newResourceData, database):
        currentResourceData = self._getResourceDataDoc(newResourceData['doc_ID'], database)
        if currentResourceData == None:
            return

        # Now delete the node_timestamp field on both document before comparing them.
        temp = {}
        temp.update(currentResourceData)
        del temp['node_timestamp']
        del newResourceData['node_timestamp']

        if temp != newResourceData:
            currentResourceData.update(newResourceData)
            try:
                log.debug("\nUpdate existing resource data from distributable\n")
                database.update([currentResourceData])
            except Exception as e:
                log.error("\n\nFailed to udpate existing resource_data doc:\n{0}".format(
                                pprint.pformat(currentResourceData)))
                log.exception(e)

    def _addResourceData(self, resourceDataCopy, database):
        # If corresponding resource_data type document is not already in the database
        # for the distributable document, create a local resource_data document and
        # add it the database
        try:
            log.debug("Adding new resource_data for distributable")
            database[resourceDataCopy['doc_ID']] = resourceDataCopy
        except Exception as e:
            log.error("\n\nCannot get current document:  {0}".format(
                            pprint.pformat(resourceDataCopy)))
            log.exception(e)

    def _handle(self, change, database):
        # The resource data copy is what the corresponding resource data document
        #  for this distributable document should look like.
        resourceDataCopy = ResourceDataModel(change['doc'])._specData
        resourceDataCopy['doc_type'] = 'resource_data'

        # Add the corresponding resource data document to database
        # if not already in the database.  Otherwise try to update.
        if not resourceDataCopy['doc_ID'] in database:
            self._addResourceData(resourceDataCopy, database )
        else:
            self._updateResourceData(resourceDataCopy, database)

class RecordResourceDataChange(DatabaseChangeHandler):
    def _canHandle(self, change, database):
        return _resourceDataPredicate(change, database)

    def _updateDistributableData(self, newDistributableData, database):
        # Use the ResourceDataModel class to create an object that
        # contains only a the resource_data spec data.
        currentDistributable = database[newDistributableData['_id']]
        temp = ResourceDataModel(currentDistributable)._specData
        del temp['node_timestamp']

        if newDistributableData != temp:
            currentDistributable.update(newDistributableData)
            log.debug("\n\nUpdate distribuatable doc:\n")
            log.debug("\n{0}\n\n".format(pprint.pformat(currentDistributable)))
            try:
                database.update([currentDistributable])
            except Exception as e:
                log.error("Failed to update existing distributable doc: {0}".format(
                                pprint.pformat(currentDistributable)))
                log.exception(e)

    def _addDistributableData(self, distributableData, database):
        try:
            log.debug('Adding distributable doc %s...\n' % distributableData['_id'])
            database[distributableData['_id']] = distributableData
        except Exception as e:
            log.error("Cannot save distributable document %s\n" % distributableData['_id'] )
            log.exception(e)

    def _handle(self, change, database):

        # Use the ResourceDataModel class to create an object that
        # contains only a the resource_data spec data.
        distributableDoc = ResourceDataModel(change['doc'])._specData
        #remove the node timestamp
        del distributableDoc['node_timestamp']
        #change thet doc_type
        distributableDoc['doc_type']='resource_data_distributable'
        distributableDoc['_id'] = change['doc']['_id']+"-distributable"

        # Check to see if a corresponding distributatable document exist.
        # not create a new distribuation document without the
        # node_timestamp and _id+distributable.
        if not distributableDoc['_id'] in database:
            self._addDistributableData(distributableDoc, database)
        else:
            self._updateDistributableData(distributableDoc, database)

# Create an handler to simply track the process change sequence in database.
# We cannot use the one for the change monitor because it is running on different
# processess.
class TrackLastSequence(DatabaseChangeThresholdHandler):
    _LAST_CHANGE_SEQ = "sequence_number"
    def __init__(self, sequenceChangeDocId, changeSaveThreshold=25):
        DatabaseChangeThresholdHandler.__init__(self,
                                                                             None,
                                                                             None,
                                                                             changeSaveThreshold,
                                                                             timedelta(seconds=5))
        self._sequenceChangeDocId =sequenceChangeDocId

    def _saveSequence(self, sequence, database):
        log.debug("Last process change sequence: {0}".format(sequence))
        doc ={"_id":self._sequenceChangeDocId,
                    self._LAST_CHANGE_SEQ : sequence}
        try:
            if self._sequenceChangeDocId in database:
                del database[self._sequenceChangeDocId]
            database[self._sequenceChangeDocId] = doc
        except Exception as e:
            log.error("\n\nError while saving {0} for dabase{1} \n".format(
                    self._sequenceChangeDocId,  str(database)))
            log.exception(e)

    def _canHandle(self, change, database):
        return (('doc' in change) and not self._sequenceChangeDocId in change['doc'])

    def _handle(self, change, database):
         if 'seq' in change:
             # Save the sequence before the last to ensure that all the change before
             # the save sequence number has been already processed.
             self._changeCount = self._changeCount + 1
             if self._shouldTakeAction():
                 self._saveSequence(change['seq'], database)
                 self._resetChangeToThreshold()

_RESOURCE_DATA_CHANGE_ID =  "Resource Data Last Processed Change Sequence"
changeTracker = TrackLastSequence( _RESOURCE_DATA_CHANGE_ID)

_RESOURCE_DATA_CHANGE_HANDLERS=[
    RecordDistributableChange(),
    RecordResourceDataChange(),
    DatabaseChangeThresholdHandler( _resourceDataPredicate,
                                                              _updateDatabaseViews,
                                                              appConfig['couchdb.threshold.viewupdate']),
    DatabaseChangeThresholdHandler(_resourceDataPredicate,
                                                            _doDistribute,
                                                             appConfig['couchdb.threshold.distributes']),
    changeTracker
    ]

def monitorResourceDataChanges():
    lastSavedSequence = -1
    if _RESOURCE_DATA_CHANGE_ID in ResourceDataModel._defaultDB:
        lastSavedSequence=ResourceDataModel._defaultDB[_RESOURCE_DATA_CHANGE_ID][TrackLastSequence._LAST_CHANGE_SEQ]

    options = {'since':lastSavedSequence}
    log.debug("\n\n-----"+pprint.pformat(options)+"------\n\n")

    changeMonitor = MonitorDatabaseChanges(appConfig['couchdb.url'],
                                                                            appConfig['couchdb.db.resourcedata'],
                                                                           _RESOURCE_DATA_CHANGE_HANDLERS,
                                                                            options)
    changeMonitor.start()
    #changeMonitor.start(threading.current_thread())
    def atExitHandler():
        changeMonitor.terminate()

    atexit.register(atExitHandler)

if __name__ == "__main__":
    logging.basicConfig()
    monitorResourceDataChanges()
<commit_msg>fix for needless db indexing churn<commit_after>#!/usr/bin/python

#!/usr/bin/pyton

#    Copyright 2011 Lockheed Martin

'''
Created on August 19, 2011

@author: jpoyau
'''
import urllib2
import logging
from lr.lib.couch_change import *
from resource_data import appConfig, ResourceDataModel
from node import NodeModel
import threading
import multiprocessing
import ctypes
import pprint
import atexit
import json
from datetime import timedelta
from pylons import config
appConfig = config['app_conf']

log = logging.getLogger(__name__)

def _resourceDataPredicate(change, database):
    try:
      return "doc" in change and change["doc"]["doc_type"] == "resource_data"
    except:
        return False

def _distributableDataPredicate(change, database):
    try:
      return "doc" in change and change["doc"]["doc_type"] == "resource_data_distributable"
    except:
        return False

def _updateDatabaseViews(change, database):
    try:
        designDocs = database.view('_all_docs',include_docs=True,
                                                        startkey='_design%2F',endkey='_design0')
        for designDoc in designDocs:
            if designDoc.doc.has_key('views') and len(designDoc.doc['views']) > 0:
                viewName = "{0}/_view/{1}".format(designDoc.id,designDoc.doc['views'].keys()[0])
                log.debug('start view update %s' % viewName)
                log.debug(len(database.view(viewName)))
    except Exception as e:
        log.error(e)

def _doDistribute(change, database):

    log.debug('start distribute')
    data = json.dumps({"dist":"dist"})
    request = urllib2.Request(appConfig['lr.distribute.url'],data,{'Content-Type':'application/json; charset=utf-8'})
    log.debug(pprint.pformat(request))
    response = urllib2.urlopen(request)
    log.debug('end distribute')

class RecordDistributableChange(DatabaseChangeHandler):
    def _canHandle(self, change, database):
        return _distributableDataPredicate(change, database)

    def _getResourceDataDoc(self, docID, database):
        # There exist already a resource_data document for the distributable
        # get it and see if it needs to be updated.
        try:
            return  ResourceDataModel(database[docID])._specData
        except Exception as e:
            log.error("\n\nCannot find existing resource_data doc for distributable: \n{0}".format(
                            docID))
            log.exception(e)
            return None

    def _updateResourceData(self, newResourceData, database):
        currentResourceData = self._getResourceDataDoc(newResourceData['doc_ID'], database)
        if currentResourceData == None:
            return

        # Now delete the node_timestamp field on both document before comparing them.
        temp = {}
        temp.update(currentResourceData)
        del temp['node_timestamp']
        del newResourceData['node_timestamp']

        if temp != newResourceData:
            currentResourceData.update(newResourceData)
            try:
                log.debug("\nUpdate existing resource data from distributable\n")
                database.update([currentResourceData])
            except Exception as e:
                log.error("\n\nFailed to udpate existing resource_data doc:\n{0}".format(
                                pprint.pformat(currentResourceData)))
                log.exception(e)

    def _addResourceData(self, resourceDataCopy, database):
        # If corresponding resource_data type document is not already in the database
        # for the distributable document, create a local resource_data document and
        # add it the database
        try:
            log.debug("Adding new resource_data for distributable")
            database[resourceDataCopy['doc_ID']] = resourceDataCopy
        except Exception as e:
            log.error("\n\nCannot get current document:  {0}".format(
                            pprint.pformat(resourceDataCopy)))
            log.exception(e)

    def _handle(self, change, database):
        # The resource data copy is what the corresponding resource data document
        #  for this distributable document should look like.
        resourceDataCopy = ResourceDataModel(change['doc'])._specData
        resourceDataCopy['doc_type'] = 'resource_data'

        # Add the corresponding resource data document to database
        # if not already in the database.  Otherwise try to update.
        if not resourceDataCopy['doc_ID'] in database:
            self._addResourceData(resourceDataCopy, database )
        else:
            self._updateResourceData(resourceDataCopy, database)

class RecordResourceDataChange(DatabaseChangeHandler):
    def _canHandle(self, change, database):
        return _resourceDataPredicate(change, database)

    def _updateDistributableData(self, newDistributableData, database):
        # Use the ResourceDataModel class to create an object that
        # contains only a the resource_data spec data.
        currentDistributable = database[newDistributableData['_id']]
        temp = ResourceDataModel(currentDistributable)._specData
        del temp['node_timestamp']

        if newDistributableData != temp:
            currentDistributable.update(newDistributableData)
            log.debug("\n\nUpdate distribuatable doc:\n")
            log.debug("\n{0}\n\n".format(pprint.pformat(currentDistributable)))
            try:
                database.update([currentDistributable])
            except Exception as e:
                log.error("Failed to update existing distributable doc: {0}".format(
                                pprint.pformat(currentDistributable)))
                log.exception(e)

    def _addDistributableData(self, distributableData, database):
        try:
            log.debug('Adding distributable doc %s...\n' % distributableData['_id'])
            database[distributableData['_id']] = distributableData
        except Exception as e:
            log.error("Cannot save distributable document %s\n" % distributableData['_id'] )
            log.exception(e)

    def _handle(self, change, database):

        # Use the ResourceDataModel class to create an object that
        # contains only a the resource_data spec data.
        distributableDoc = ResourceDataModel(change['doc'])._specData
        #remove the node timestamp
        del distributableDoc['node_timestamp']
        #change thet doc_type
        distributableDoc['doc_type']='resource_data_distributable'
        distributableDoc['_id'] = change['doc']['_id']+"-distributable"

        # Check to see if a corresponding distributatable document exist.
        # not create a new distribuation document without the
        # node_timestamp and _id+distributable.
        if not distributableDoc['_id'] in database:
            self._addDistributableData(distributableDoc, database)
        else:
            self._updateDistributableData(distributableDoc, database)

# Create an handler to simply track the process change sequence in database.
# We cannot use the one for the change monitor because it is running on different
# processess.
class TrackLastSequence(DatabaseChangeThresholdHandler):
    _LAST_CHANGE_SEQ = "sequence_number"
    def __init__(self, sequenceChangeDocId, changeSaveThreshold=25):
        DatabaseChangeThresholdHandler.__init__(self,
                                                                             None,
                                                                             None,
                                                                             changeSaveThreshold,
                                                                             timedelta(seconds=5))
        self._sequenceChangeDocId =sequenceChangeDocId

    def _saveSequence(self, sequence, database):
        log.debug("Last process change sequence: {0}".format(sequence))
        doc ={"_id":self._sequenceChangeDocId,
                    self._LAST_CHANGE_SEQ : sequence}
        try:
            if self._sequenceChangeDocId in database:
                del database[self._sequenceChangeDocId]
            database[self._sequenceChangeDocId] = doc
        except Exception as e:
            log.error("\n\nError while saving {0} for dabase{1} \n".format(
                    self._sequenceChangeDocId,  str(database)))
            log.exception(e)

    def _canHandle(self, change, database):
        return (('doc' in change) and not self._sequenceChangeDocId in change['doc'])

    def _handle(self, change, database):
         if 'seq' in change:
             # Save the sequence before the last to ensure that all the change before
             # the save sequence number has been already processed.
             self._changeCount = self._changeCount + 1
             if self._shouldTakeAction():
                 self._saveSequence(change['seq'], database)
                 self._resetChangeToThreshold()

_RESOURCE_DATA_CHANGE_ID =  "_local/Resource Data Last Processed Change Sequence"
changeTracker = TrackLastSequence( _RESOURCE_DATA_CHANGE_ID)

_RESOURCE_DATA_CHANGE_HANDLERS=[
    RecordDistributableChange(),
    RecordResourceDataChange(),
    DatabaseChangeThresholdHandler( _resourceDataPredicate,
                                                              _updateDatabaseViews,
                                                              appConfig['couchdb.threshold.viewupdate']),
    DatabaseChangeThresholdHandler(_resourceDataPredicate,
                                                            _doDistribute,
                                                             appConfig['couchdb.threshold.distributes']),
    changeTracker
    ]

def monitorResourceDataChanges():
    lastSavedSequence = -1
    if _RESOURCE_DATA_CHANGE_ID in ResourceDataModel._defaultDB:
        lastSavedSequence=ResourceDataModel._defaultDB[_RESOURCE_DATA_CHANGE_ID][TrackLastSequence._LAST_CHANGE_SEQ]

    options = {'since':lastSavedSequence}
    log.debug("\n\n-----"+pprint.pformat(options)+"------\n\n")

    changeMonitor = MonitorDatabaseChanges(appConfig['couchdb.url'],
                                                                            appConfig['couchdb.db.resourcedata'],
                                                                           _RESOURCE_DATA_CHANGE_HANDLERS,
                                                                            options)
    changeMonitor.start()
    #changeMonitor.start(threading.current_thread())
    def atExitHandler():
        changeMonitor.terminate()

    atexit.register(atExitHandler)

if __name__ == "__main__":
    logging.basicConfig()
    monitorResourceDataChanges()
<|endoftext|>