~ubuntu-branches/ubuntu/vivid/mago/vivid

« back to all changes in this revision

Viewing changes to mago/magomatic.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright (C) 2009-2011 Canonical Ltd
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
 
 
19
import os
 
20
import re
 
21
import sys
 
22
import ldtp
 
23
import ooldtp
 
24
import shutil
 
25
from time import sleep
 
26
from magomaticconfig import get_data_file, get_data_path
 
27
 
 
28
class Magomatic:
 
29
    """
 
30
    """
 
31
 
 
32
    def __init__(self, launcher, main_window, child_window = None, \
 
33
                 overwrite = False, white_list=[]):
 
34
        self.launcher = launcher
 
35
        if child_window:
 
36
            appname = main_window
 
37
        else:
 
38
            appname = launcher
 
39
        self.appname = re.sub('\W', '_', appname.lower())
 
40
        self.overwrite = overwrite
 
41
        self.main_window = main_window
 
42
        self.child_window = child_window
 
43
        self.white_list = ["check_box", "check_menu_item", "combo_box", "entry",
 
44
                           "icon", "list", "menu", "menu_item", "page_tab",
 
45
                           "page_tab_list", "push_button", "radio_button",
 
46
                           "radio_menu_item", "scroll_bar", "spin_button",
 
47
                           "table", "text", "toggle_button", "tree", "tree_table"]
 
48
        self.app_folder = get_data_file(launcher)
 
49
 
 
50
    def _launchapp(self, progname):
 
51
        '''returns the window name while ldtp.launchapp returns the PID'''
 
52
        winlist=ldtp.getwindowlist()
 
53
        timeout=60
 
54
        ldtp.launchapp(progname)
 
55
 
 
56
        # Any way to get the window name from the app name ?
 
57
        # appundertest is not implemented in LDTPv2
 
58
        wincount=len(winlist)
 
59
        wincount_orig = wincount
 
60
        while wincount == wincount_orig and timeout > 0:
 
61
            winlist = ldtp.getwindowlist()
 
62
            wincount = len(winlist)
 
63
            timeout -= 1
 
64
            sleep(1)
 
65
 
 
66
        if timeout <= 0:
 
67
            print "Failed to identify window name for '%s'" % progname
 
68
            sys.exit(2)
 
69
 
 
70
        return(winlist[-1])
 
71
 
 
72
    def create_app_folder(self):
 
73
        return
 
74
 
 
75
        template = get_data_file("template")
 
76
        substitutions = (("name_application", self.appname),
 
77
                        ("NameApplication", self.appname.capitalize()),
 
78
                        ("Application Basics", self.appname.capitalize()),
 
79
                        )
 
80
 
 
81
        if self.child_window:
 
82
            leafs = ('application')
 
83
        else:
 
84
            leafs = ('application','test_suite', self.appname)
 
85
        for leaf in leafs:
 
86
            try:
 
87
                os.makedirs(os.path.join(self.app_folder, leaf))
 
88
            except OSError, e:
 
89
                if e.errno != 17: # File exists
 
90
                    raise OSError, e
 
91
 
 
92
        for dirpath, dirnames, filenames in os.walk(template):
 
93
            for filename in filenames:
 
94
                target_file = filename
 
95
                for pattern, sub in substitutions:
 
96
                    target_file = target_file.replace(pattern, sub)
 
97
 
 
98
                file_path = os.path.join(dirpath, filename)
 
99
                relpath = os.path.dirname(os.path.relpath(file_path, template))
 
100
 
 
101
                for pattern, sub in substitutions:
 
102
                    relpath = relpath.replace(pattern, sub)
 
103
 
 
104
                if self.child_window and (relpath == 'test_suite' or \
 
105
                                          relpath == self.appname):
 
106
                    continue
 
107
 
 
108
                file_des = open(file_path,'r')
 
109
                file_contents = file_des.read()
 
110
 
 
111
                for s in substitutions:
 
112
                    pattern, sub = s
 
113
                    file_contents = file_contents.replace(pattern,sub)
 
114
 
 
115
                target_path = os.path.join(self.app_folder, relpath + "/" + target_file)
 
116
                if (not self.overwrite) and os.path.exists(target_path):
 
117
                    print "Failed to add file to project\n cannot add: %s - this file already exists." % target_path
 
118
                    sys.exit(4)
 
119
 
 
120
                file_des = open(target_path, "w")
 
121
                file_des.write(file_contents)
 
122
 
 
123
    def discover_application(self, tree = False):
 
124
        """Dump the list of components of the application
 
125
 
 
126
        :param tree: If True, dump the component tree instead of the
 
127
                     application map
 
128
        """
 
129
 
 
130
        if self.launcher and not self.main_window:
 
131
            # Only launcher pass as argument  we try to guess the name of the main windows
 
132
            self.main_window = self._launchapp(self.launcher)
 
133
        elif not ldtp.guiexist(self.main_window):
 
134
            ldtp.launchapp(self.launcher)
 
135
        ldtp.waittillguiexist(self.main_window)
 
136
        app = ooldtp.context(self.main_window)
 
137
 
 
138
        if tree:
 
139
            self.walk_application(self.main_window)
 
140
        else:
 
141
 
 
142
            constants = ""
 
143
            if not self.child_window:
 
144
                # If main window, then added launcher
 
145
                constants += "launcher = '%s'\n" % self.launcher
 
146
                constants += "launcher_args = []\n"
 
147
            constants += "window_name = '%s'\n\n" % re.sub('\W', '*', self.main_window)
 
148
 
 
149
            objectlist = sorted(app.getobjectlist())
 
150
            for component in objectlist:
 
151
                role = ldtp.getobjectproperty(self.main_window, component, 'class')
 
152
                component = component.encode("UTF-8")
 
153
 
 
154
                constant = ""
 
155
 
 
156
                if role in self.white_list:
 
157
                    #constant = re.sub('\W', '_', component.upper())
 
158
                    #constant = constant[:3] + "_" + constant[3:] # Split prefix for clarity
 
159
                    #constants += "    " + constant +" = _('" + re.sub('\W', '*', component) + "')\n"
 
160
                    # All is commented by default
 
161
                    constants += '#%s = "%s"\n' % (re.sub('\W', '_', component), component)
 
162
 
 
163
            print constants
 
164
        #app_file = os.path.join(self.app_folder, "application/" + self.appname + ".py")
 
165
 
 
166
        #read_file = open(app_file, "r")
 
167
        #contents = read_file.read()
 
168
 
 
169
        #contents = contents.replace("CONSTANTS", constants)
 
170
 
 
171
        #write_file = open(app_file, "w")
 
172
        #write_file.write(contents)
 
173
        #write_file.close()
 
174
 
 
175
        #if 'mnuQuit' in objectlist:
 
176
        ldtp.closewindow(self.main_window)
 
177
        ldtp.waittillguinotexist (self.main_window)
 
178
 
 
179
    def walk_application(self, node = None, level = 1):
 
180
        """Walk the component tree of the application
 
181
 
 
182
        :param node: Name of the parent node
 
183
        :param level: Level of the node
 
184
        """
 
185
 
 
186
        if not node: return
 
187
        if level == 1: print node
 
188
 
 
189
        children = ldtp.getobjectproperty(self.main_window, node,
 
190
                                          'children').split(' ')
 
191
 
 
192
        for child in children:
 
193
            if not child: continue
 
194
            print "%s|__ %s" % ("|   " * (level - 1), child)
 
195
            self.walk_application(child, level + 1)