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

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

from __future__ import absolute_import
from __future__ import print_function

import pickle
import unittest

from gaphas.canvas import Canvas
from gaphas.examples import Box
from gaphas.item import Element, Line
from gaphas.view import View, GtkView


# Ensure extra pickle reducers/reconstructors are loaded:
import gaphas.picklers


class MyPickler(pickle.Pickler):
    def save(self, obj):
        print('saving obj', obj, type(obj))
        try:
            return pickle.Pickler.save(self, obj)
        except pickle.PicklingError as e:
            print('Error while pickling', obj, self.dispatch.get(type(obj)))
            raise e


class my_disconnect(object):
    """
    Disconnect object should be located at top-level, so the pickle code
    can find it.
    """

    def __call__(self):
        pass


def create_canvas():
    canvas = Canvas()
    box = Box()
    canvas.add(box)
    box.matrix.translate(100, 50)
    box.matrix.rotate(50)
    box2 = Box()
    canvas.add(box2, parent=box)

    line = Line()
    line.handles()[0].visible = False
    line.handles()[0].connected_to = box
    line.handles()[0].disconnect = my_disconnect()
    line.handles()[0].connection_data = 1

    canvas.add(line)

    canvas.update()

    return canvas


class PickleTestCase(unittest.TestCase):
    def test_pickle_element(self):
        item = Element()

        pickled = pickle.dumps(item)
        i2 = pickle.loads(pickled)

        assert i2
        assert len(i2.handles()) == 4

    def test_pickle_line(self):
        item = Line()

        pickled = pickle.dumps(item)
        i2 = pickle.loads(pickled)

        assert i2
        assert len(i2.handles()) == 2

    def test_pickle(self):
        canvas = create_canvas()

        pickled = pickle.dumps(canvas)
        c2 = pickle.loads(pickled)

        assert type(canvas._tree.nodes[0]) is Box
        assert type(canvas._tree.nodes[1]) is Box
        assert type(canvas._tree.nodes[2]) is Line

    def test_pickle_connect(self):
        """
        Persist a connection.
        """
        canvas = Canvas()
        box = Box()
        canvas.add(box)
        box2 = Box()
        canvas.add(box2, parent=box)

        line = Line()
        line.handles()[0].visible = False
        line.handles()[0].connected_to = box
        line.handles()[0].disconnect = my_disconnect()
        line.handles()[0].connection_data = 1

        canvas.add(line)

        pickled = pickle.dumps(canvas)
        c2 = pickle.loads(pickled)

        assert type(canvas._tree.nodes[0]) is Box
        assert type(canvas._tree.nodes[1]) is Box
        assert type(canvas._tree.nodes[2]) is Line
        assert c2.solver

        line2 = c2._tree.nodes[2]
        h = line2.handles()[0]
        assert h.visible == False
        assert h.connected_to is c2._tree.nodes[0]

        # connection_data and disconnect have not been persisted
        assert h.connection_data == 1, h.connection_data
        assert h.disconnect, h.disconnect
        assert callable(h.disconnect)
        assert h.disconnect() is None, h.disconnect()

    def test_pickle_with_view(self):
        canvas = create_canvas()

        pickled = pickle.dumps(canvas)

        c2 = pickle.loads(pickled)

        view = View(canvas=c2)

        import cairo
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0)
        cr = cairo.Context(surface)
        view.update_bounding_box(cr)
        cr.show_page()
        surface.flush()
        surface.finish()

    def test_pickle_with_gtk_view(self):
        canvas = create_canvas()

        pickled = pickle.dumps(canvas)

        c2 = pickle.loads(pickled)

        from gi.repository import Gtk
        win = Gtk.Window()
        view = GtkView(canvas=c2)
        win.add(view)

        view.show()
        win.show()

        view.update()

    def test_pickle_with_gtk_view_with_connection(self):
        canvas = create_canvas()
        box = canvas._tree.nodes[0]
        assert isinstance(box, Box)
        line = canvas._tree.nodes[2]
        assert isinstance(line, Line)

        view = GtkView(canvas=canvas)

        #        from gaphas.tool import ConnectHandleTool
        #        handle_tool = ConnectHandleTool()
        #        handle_tool.connect(view, line, line.handles()[0], (40, 0))
        #        assert line.handles()[0].connected_to is box, line.handles()[0].connected_to
        #        assert line.handles()[0].connection_data
        #        assert line.handles()[0].disconnect
        #        assert isinstance(line.handles()[0].disconnect, object), line.handles()[0].disconnect

        import io
        f = io.StringIO()
        pickler = MyPickler(f)
        pickler.dump(canvas)
        pickled = f.getvalue()

        c2 = pickle.loads(pickled)

        from gi.repository import Gtk
        win = Gtk.Window()
        view = GtkView(canvas=c2)
        win.add(view)
        view.show()
        win.show()

        view.update()

    def test_pickle_demo(self):
        import demo
<commit_msg>Fix expected string instead of bytes<commit_after>#!/usr/bin/env python

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

from __future__ import absolute_import
from __future__ import print_function

import pickle
import unittest

from gaphas.canvas import Canvas
from gaphas.examples import Box
from gaphas.item import Element, Line
from gaphas.view import View, GtkView


# Ensure extra pickle reducers/reconstructors are loaded:
import gaphas.picklers


class MyPickler(pickle.Pickler):
    def save(self, obj):
        print('saving obj', obj, type(obj))
        try:
            return pickle.Pickler.save(self, obj)
        except pickle.PicklingError as e:
            print('Error while pickling', obj, self.dispatch.get(type(obj)))
            raise e


class my_disconnect(object):
    """
    Disconnect object should be located at top-level, so the pickle code
    can find it.
    """

    def __call__(self):
        pass


def create_canvas():
    canvas = Canvas()
    box = Box()
    canvas.add(box)
    box.matrix.translate(100, 50)
    box.matrix.rotate(50)
    box2 = Box()
    canvas.add(box2, parent=box)

    line = Line()
    line.handles()[0].visible = False
    line.handles()[0].connected_to = box
    line.handles()[0].disconnect = my_disconnect()
    line.handles()[0].connection_data = 1

    canvas.add(line)

    canvas.update()

    return canvas


class PickleTestCase(unittest.TestCase):
    def test_pickle_element(self):
        item = Element()

        pickled = pickle.dumps(item)
        i2 = pickle.loads(pickled)

        assert i2
        assert len(i2.handles()) == 4

    def test_pickle_line(self):
        item = Line()

        pickled = pickle.dumps(item)
        i2 = pickle.loads(pickled)

        assert i2
        assert len(i2.handles()) == 2

    def test_pickle(self):
        canvas = create_canvas()

        pickled = pickle.dumps(canvas)
        c2 = pickle.loads(pickled)

        assert type(canvas._tree.nodes[0]) is Box
        assert type(canvas._tree.nodes[1]) is Box
        assert type(canvas._tree.nodes[2]) is Line

    def test_pickle_connect(self):
        """
        Persist a connection.
        """
        canvas = Canvas()
        box = Box()
        canvas.add(box)
        box2 = Box()
        canvas.add(box2, parent=box)

        line = Line()
        line.handles()[0].visible = False
        line.handles()[0].connected_to = box
        line.handles()[0].disconnect = my_disconnect()
        line.handles()[0].connection_data = 1

        canvas.add(line)

        pickled = pickle.dumps(canvas)
        c2 = pickle.loads(pickled)

        assert type(canvas._tree.nodes[0]) is Box
        assert type(canvas._tree.nodes[1]) is Box
        assert type(canvas._tree.nodes[2]) is Line
        assert c2.solver

        line2 = c2._tree.nodes[2]
        h = line2.handles()[0]
        assert h.visible == False
        assert h.connected_to is c2._tree.nodes[0]

        # connection_data and disconnect have not been persisted
        assert h.connection_data == 1, h.connection_data
        assert h.disconnect, h.disconnect
        assert callable(h.disconnect)
        assert h.disconnect() is None, h.disconnect()

    def test_pickle_with_view(self):
        canvas = create_canvas()

        pickled = pickle.dumps(canvas)

        c2 = pickle.loads(pickled)

        view = View(canvas=c2)

        import cairo
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0)
        cr = cairo.Context(surface)
        view.update_bounding_box(cr)
        cr.show_page()
        surface.flush()
        surface.finish()

    def test_pickle_with_gtk_view(self):
        canvas = create_canvas()

        pickled = pickle.dumps(canvas)

        c2 = pickle.loads(pickled)

        from gi.repository import Gtk
        win = Gtk.Window()
        view = GtkView(canvas=c2)
        win.add(view)

        view.show()
        win.show()

        view.update()

    def test_pickle_with_gtk_view_with_connection(self):
        canvas = create_canvas()
        box = canvas._tree.nodes[0]
        assert isinstance(box, Box)
        line = canvas._tree.nodes[2]
        assert isinstance(line, Line)

        view = GtkView(canvas=canvas)

        #        from gaphas.tool import ConnectHandleTool
        #        handle_tool = ConnectHandleTool()
        #        handle_tool.connect(view, line, line.handles()[0], (40, 0))
        #        assert line.handles()[0].connected_to is box, line.handles()[0].connected_to
        #        assert line.handles()[0].connection_data
        #        assert line.handles()[0].disconnect
        #        assert isinstance(line.handles()[0].disconnect, object), line.handles()[0].disconnect

        import io
        f = io.BytesIO()
        pickler = MyPickler(f)
        pickler.dump(canvas)
        pickled = f.getvalue()

        c2 = pickle.loads(pickled)

        from gi.repository import Gtk
        win = Gtk.Window()
        view = GtkView(canvas=c2)
        win.add(view)
        view.show()
        win.show()

        view.update()

    def test_pickle_demo(self):
        import demo

{'fraction_non_alphanumeric': 0.10415597742432016, 'fraction_numerical': 0.013169146570891055, 'mean_word_length': 2.3548479632816983, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1850998', 'n_tokens_mistral': 3658, 'n_tokens_neox': 3497, 'n_words': 921}