Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_page_details.py
blob: e3a87784da693cd0f9237025be247a6612c338c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# coding: UTF-8
# Copyright 2009 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
import os.path
import webbrowser
import ka_debug
import ka_extensionpoint
import ka_task
import ka_html_page

_DETAILS_PAGE_NUMBER = 4

class DetailsController(ka_html_page.HtmlPage):
    """
    inv: self._activity_root is not None
    inv: self._produced_files_list is not None
    inv: self._drawing_page is not None
    """

    def __init__(self, controller, widget_tree):
        """
        pre: controller is not None
        pre: widget_tree is not None
        """
        super(DetailsController, self).__init__(_DETAILS_PAGE_NUMBER,
                                              self.on_switch_page,
                                              'details_scrolledwindow',
                                              widget_tree)
        self._file_uri = ''
        self._produced_files_list = []
        self._activity_root = controller.activity_root
        self._drawing_page = self._widget_tree.get_widget('kandidNotebook'). \
                                             get_nth_page(_DETAILS_PAGE_NUMBER)

    def close(self):
        """Clean up"""
        files = [fp for fp in self._produced_files_list if os.path.isfile(fp)]
        for file_path in files:
#            print 'unlink', file_path
            os.unlink(file_path)
        folders = [fp for fp in self._produced_files_list if os.path.isdir(fp)]
        for file_path in folders:
#            print 'rmdir', file_path
            os.rmdir(file_path)

    def on_switch_page(self):
        """Only a dummy."""
        pass

    def task_explain(self, task, *args, **kwargs):
        """Render zoom view of protozoon.
        pre: len(args) >= 1
        """
        protozoon = args[0]
        ka_debug.info('task_explain entry: ')
        folder = os.path.join(self._activity_root, 'tmp')
        ep_key = 'html'
        formater = ka_extensionpoint.create('formater_'+ep_key,
                                            'index',
                                            protozoon.get_unique_id(),
                                            folder)
        protozoon.explain(task, formater)
        file_path = formater.get_absolutename(ep_key)
        formater.write_html_file(file_path)
        self._produced_files_list.extend(formater.produced_files_list)
        self._produced_files_list.append(formater.get_pathname())
        self._file_uri = 'file://' + file_path
        ka_debug.info('task_explain exit:  %s' % (self._file_uri))

    def start_calculation(self, protozoon):
        """Start explaining of a protozoon.
        pre: protozoon is not None
        """
        ka_debug.info('DetailsController.start_calculation %s' % 
                                                   (protozoon.get_unique_id()))
        task = ka_task.GeneratorTask(self.task_explain,
                                     self.on_explain_completed,
                                     'explain')
        task.start(protozoon)

    def on_explain_completed(self, *args):
        """HTML file and PNGs are created. Time to switch to a browser widget.
        """
        ka_debug.info('on_explain_completed %s' % (self._file_uri))
        if len(self._file_uri) > 0: 
            if ka_debug.locale_testrun:
                # Only for testing on my computer
                result = webbrowser.open(self._file_uri)
                ka_debug.info('webbrowser.open: [%s] %s' % 
                                                 (self._file_uri, str(result)))
            if self._htmlview is not None:
                self._htmlview.load_uri(self._file_uri)
        self._drawing_page.show()
        self._widget_tree.get_widget('kandidNotebook'). \
                                         set_current_page(_DETAILS_PAGE_NUMBER)