1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
# Copyright (C) 2009-2011 Canonical Ltd
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.
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.
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
25
from time import sleep
26
from magomaticconfig import get_data_file, get_data_path
32
def __init__(self, launcher, main_window, child_window = None, \
33
overwrite = False, white_list=[]):
34
self.launcher = 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)
50
def _launchapp(self, progname):
51
'''returns the window name while ldtp.launchapp returns the PID'''
52
winlist=ldtp.getwindowlist()
54
ldtp.launchapp(progname)
56
# Any way to get the window name from the app name ?
57
# appundertest is not implemented in LDTPv2
59
wincount_orig = wincount
60
while wincount == wincount_orig and timeout > 0:
61
winlist = ldtp.getwindowlist()
62
wincount = len(winlist)
67
print "Failed to identify window name for '%s'" % progname
72
def create_app_folder(self):
75
template = get_data_file("template")
76
substitutions = (("name_application", self.appname),
77
("NameApplication", self.appname.capitalize()),
78
("Application Basics", self.appname.capitalize()),
82
leafs = ('application')
84
leafs = ('application','test_suite', self.appname)
87
os.makedirs(os.path.join(self.app_folder, leaf))
89
if e.errno != 17: # File exists
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)
98
file_path = os.path.join(dirpath, filename)
99
relpath = os.path.dirname(os.path.relpath(file_path, template))
101
for pattern, sub in substitutions:
102
relpath = relpath.replace(pattern, sub)
104
if self.child_window and (relpath == 'test_suite' or \
105
relpath == self.appname):
108
file_des = open(file_path,'r')
109
file_contents = file_des.read()
111
for s in substitutions:
113
file_contents = file_contents.replace(pattern,sub)
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
120
file_des = open(target_path, "w")
121
file_des.write(file_contents)
123
def discover_application(self, tree = False):
124
"""Dump the list of components of the application
126
:param tree: If True, dump the component tree instead of the
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)
139
self.walk_application(self.main_window)
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)
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")
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)
164
#app_file = os.path.join(self.app_folder, "application/" + self.appname + ".py")
166
#read_file = open(app_file, "r")
167
#contents = read_file.read()
169
#contents = contents.replace("CONSTANTS", constants)
171
#write_file = open(app_file, "w")
172
#write_file.write(contents)
175
#if 'mnuQuit' in objectlist:
176
ldtp.closewindow(self.main_window)
177
ldtp.waittillguinotexist (self.main_window)
179
def walk_application(self, node = None, level = 1):
180
"""Walk the component tree of the application
182
:param node: Name of the parent node
183
:param level: Level of the node
187
if level == 1: print node
189
children = ldtp.getobjectproperty(self.main_window, node,
190
'children').split(' ')
192
for child in children:
193
if not child: continue
194
print "%s|__ %s" % ("| " * (level - 1), child)
195
self.walk_application(child, level + 1)