Skip to content
This repository has been archived by the owner on Jul 31, 2018. It is now read-only.

Commit

Permalink
New code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
moden-py committed Nov 1, 2015
1 parent 7168ec5 commit efab8ad
Show file tree
Hide file tree
Showing 8 changed files with 2,241 additions and 363 deletions.
251 changes: 186 additions & 65 deletions _mainframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import locale
import platform
import thread
import traceback
import wx

import code_manager
import proxy

#Avoid limit of wx.ListCtrl in 512 symbols
Expand All @@ -35,11 +37,13 @@
def create(parent):
return Frame1(parent)


[wxID_FRAME1, wxID_FRAME1LISTCTRL1_PROPERTIES, wxID_FRAME1STATICBOX_EDITOR,
wxID_FRAME1STATICBOX_OBJECTSBROWSER, wxID_FRAME1STATICBOX_PROPRTIES,
wxID_FRAME1TEXTCTRL_EDITOR, wxID_FRAME1TREECTRL_OBJECTSBROWSER
] = [wx.NewId() for _init_ctrls in range(7)]


class Frame1(wx.Frame):
"""
Main application frame
Expand Down Expand Up @@ -76,14 +80,16 @@ def _init_ctrls(self, prnt):
name='treeCtrl_ObjectsBrowser', parent=self, style=wx.TR_HAS_BUTTONS)

self.treeCtrl_ObjectsBrowser.Bind(wx.EVT_TREE_SEL_CHANGED,
self.OnTreeCtrl1TreeSelChanged, id=wxID_FRAME1TREECTRL_OBJECTSBROWSER)
self.ObjectsBrowserSelChanged, id=wxID_FRAME1TREECTRL_OBJECTSBROWSER)

self.treeCtrl_ObjectsBrowser.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.ObjectsBrowserRight_Click)
self.treeCtrl_ObjectsBrowser.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.ObjectsBrowserRightClick)
#----------

#-----Editor-----
self.textCtrl_Editor = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL_EDITOR,
name='textCtrl_Editor', parent=self, style=wx.TE_MULTILINE | wx.TE_READONLY, value='')

self.textCtrl_Editor.Bind(wx.EVT_CONTEXT_MENU, self.EditorContextMenu)

self.textCtrl_Editor.SetInitialSize((300,250))
#----------
Expand All @@ -99,7 +105,7 @@ def _init_ctrls(self, prnt):
heading='Value', width=-1)

self.listCtrl_Properties.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK,
self.OnlistCtrl_PropertiesListItemRightClick, id=wxID_FRAME1LISTCTRL1_PROPERTIES)
self.PropertiesRightClick, id=wxID_FRAME1LISTCTRL1_PROPERTIES)

#self.listCtrl_Properties.Bind(wx.EVT_LEFT_DCLICK, self.Refresh, id=wxID_FRAME1LISTCTRL1_PROPERTIES)
#----------
Expand Down Expand Up @@ -127,13 +133,13 @@ def _init_ctrls(self, prnt):

def __init__(self, parent):
self._init_ctrls(parent)
self._init_windows_tree()
self.textCtrl_Editor.AppendText('import pywinauto\n\n')
self.textCtrl_Editor.AppendText('pwa_app = pywinauto.application.Application()\n')
self._init_windows_tree()
self.textCtrl_Editor.SetForegroundColour(wx.LIGHT_GREY)
self.textCtrl_Editor.AppendText('#Perform an action - right click on item in the object browser.')
self.prop_updater = prop_viewer_updater(self.listCtrl_Properties)
self.tree_updater = tree_updater(self.treeCtrl_ObjectsBrowser)

def OnTreeCtrl1TreeSelChanged(self, event):
def ObjectsBrowserSelChanged(self, event):
tree_item = event.GetItem()
obj = self.treeCtrl_ObjectsBrowser.GetItemData(tree_item).GetData()
if not obj._check_existence():
Expand All @@ -144,105 +150,209 @@ def OnTreeCtrl1TreeSelChanged(self, event):
self.tree_updater.tree_update(tree_item, obj)
obj.Highlight_control()

def ObjectsBrowserRight_Click(self, event):
def ObjectsBrowserRightClick(self, event):
menu = wx.Menu()
#tree_item = self.treeCtrl_ObjectsBrowser.GetSelection()
tree_item = event.GetItem()
obj = self.treeCtrl_ObjectsBrowser.GetItemData(tree_item).GetData()
self.GLOB_last_rclick_tree_obj = obj
#self.treeCtrl_ObjectsBrowser.SelectItem(tree_item)
if obj._check_existence():
actions = obj.Get_actions()
if actions:
for id, action_name in actions:
menu.Append(id, action_name)
if not obj._check_actionable():
menu.Enable(id, False)
else:
menu.Append(0, 'No actions')
menu.Enable(0, False)
self.PopupMenu(menu)
menu.Destroy()
actions = obj.Get_actions()
extended_actions = obj.Get_extended_actions()
if not actions and not extended_actions:
menu.Append(0, 'No actions')
menu.Enable(0, False)
else:
if extended_actions:
for _id, extended_action_name in extended_actions:
menu.Append(_id, extended_action_name)
if not obj._check_actionable():
menu.Enable(_id, False)
menu.AppendSeparator()

for _id, action_name in actions:
menu.Append(_id, action_name)
if not obj._check_actionable():
menu.Enable(_id, False)

self.PopupMenu(menu)
menu.Destroy()
else:
self._init_windows_tree()
tree_item = self.treeCtrl_ObjectsBrowser.GetRootItem()
obj = self.treeCtrl_ObjectsBrowser.GetItemData(tree_item).GetData()
self.prop_updater.props_update(obj)
self.tree_updater.tree_update(tree_item, obj)
self._init_windows_tree()
tree_item = self.treeCtrl_ObjectsBrowser.GetRootItem()
obj = self.treeCtrl_ObjectsBrowser.GetItemData(tree_item).GetData()
self.prop_updater.props_update(obj)
self.tree_updater.tree_update(tree_item, obj)

def OnlistCtrl_PropertiesListItemRightClick(self, event):
def PropertiesRightClick(self, event):
self.GLOB_prop_item_index = event.GetIndex()
menu = wx.Menu()
menu.Append(201, 'Copy all')
menu.AppendSeparator()
menu.Append(202, 'Copy property')
menu.Append(203, 'Copy value')
menu.Append(204, 'Copy unicode value')
self.PopupMenu(menu)
menu.Destroy()
for _id, option_name in sorted(const.PROPERTIES_ACTIONS.items()):
if option_name:
menu.Append(_id, option_name)
else:
menu.AppendSeparator()
self.PopupMenu(menu)
menu.Destroy()

def EditorContextMenu(self, event):
cm = code_manager.CodeManager()
menu = wx.Menu()

for _id, option_name in sorted(const.EDITOR_ACTIONS.items()):
if option_name:
menu.Append(_id, option_name)
if not cm: # empty code
menu.Enable(_id, False)
else:
menu.AppendSeparator()
if not self.textCtrl_Editor.GetStringSelection(): # empty selection
menu.Enable(404, False) # 404: 'Copy'

self.PopupMenu(menu)
menu.Destroy()

def menu_action(self, event):
id = event.Id
#print id
if 99 < id < 200:
#object browser menu
self.make_action(id)
elif 199 < id < 300:
#properties viewer menu
self.clipboard_action(id)
menu_id = event.Id
if menu_id in const.ACTIONS or \
menu_id in const.EXTENDED_ACTIONS:
# object browser menu
# regular action or extended action
self.make_action(menu_id)

elif menu_id in const.PROPERTIES_ACTIONS:
# properties viewer menu
self.properties_action(menu_id)

elif menu_id in const.EDITOR_ACTIONS:
# editor menu
self.editor_action(menu_id)

else:
#Unknown menu id
pass
raise RuntimeError("Unknown menu_id=%s for properties "
"menu" % menu_id)

def clipboard_action(self, menu_id):
def properties_action(self, menu_id):
item = self.GLOB_prop_item_index
clipdata = wx.TextDataObject()
if menu_id == 201:
# Copy all

if 'Copy all' == const.PROPERTIES_ACTIONS[menu_id]:
all_texts = ''
items_count = self.listCtrl_Properties.GetItemCount()
for i in range(items_count):
prop_name = self.listCtrl_Properties.GetItem(i, 0).GetText()
val_name = self.listCtrl_Properties.GetItem(i, 1).GetText()
all_texts += '%s : %s' % (prop_name, val_name) + '\n'
clipdata.SetText(all_texts)
elif menu_id == 202:
# Copy property

elif 'Copy property' == const.PROPERTIES_ACTIONS[menu_id]:
property = self.listCtrl_Properties.GetItem(item,0).GetText()
clipdata.SetText(property)
elif menu_id == 203:
# Copy value

elif 'Copy value' == const.PROPERTIES_ACTIONS[menu_id]:
#value = self.listCtrl_Properties.GetItem(item,1).GetText()
key = self.listCtrl_Properties.GetItem(item,0).GetText()
try:
value_str = str(PROPERTIES[key])
except exceptions.UnicodeEncodeError:
value_str = PROPERTIES[key].encode(locale.getpreferredencoding(), 'replace')
value_str = PROPERTIES[key].encode(
locale.getpreferredencoding(), 'replace')
clipdata.SetText(value_str)
elif menu_id == 204:
# Copy unicode value

elif 'Copy unicode value' == const.PROPERTIES_ACTIONS[menu_id]:
key = self.listCtrl_Properties.GetItem(item,0).GetText()
try:
value_unicode_escape = str(PROPERTIES[key])
except exceptions.UnicodeEncodeError:
value_unicode_escape = PROPERTIES[key].encode('unicode-escape', 'replace')
value_unicode_escape = PROPERTIES[key].encode('unicode-escape',
'replace')
clipdata.SetText(value_unicode_escape)
else:
#Unknow id
pass
raise RuntimeError("Unknown menu_id=%s for properties "
"menu" % menu_id)

self.GLOB_prop_item_index = None
wx.TheClipboard.Open()
wx.TheClipboard.SetData(clipdata)
wx.TheClipboard.Close()



def make_action(self, menu_id):
#tree_item = self.treeCtrl_ObjectsBrowser.GetSelection()
#obj = self.treeCtrl_ObjectsBrowser.GetItemData(tree_item).GetData()
obj = self.GLOB_last_rclick_tree_obj
self.textCtrl_Editor.AppendText(obj.Get_code(menu_id))
obj.Exec_action(menu_id)


if menu_id in const.ACTIONS:
# Regular action
action = const.ACTIONS[menu_id]
try:
code = obj.Get_code(action)
obj.Exec_action(action)
except:
code = None
dlg = wx.MessageDialog(self, traceback.format_exc(5),
'Warning!', wx.OK | wx.ICON_WARNING)
dlg.ShowModal()
dlg.Destroy()

elif menu_id in const.EXTENDED_ACTIONS:
# Extended action
try:
obj.SetCodestyle(menu_id)
code = obj.Get_code()
except:
code = None
dlg = wx.MessageDialog(self, traceback.format_exc(5),
'Warning!', wx.OK | wx.ICON_WARNING)
dlg.ShowModal()
dlg.Destroy()

if code is not None:
self.textCtrl_Editor.SetForegroundColour(wx.BLACK)
self.textCtrl_Editor.SetValue(code)

def editor_action(self, menu_id):
cm = code_manager.CodeManager()

if 'Clear last command' == const.EDITOR_ACTIONS[menu_id]:
cm.clear_last()
self.textCtrl_Editor.SetValue(cm.get_full_code())

elif 'Clear the code' == const.EDITOR_ACTIONS[menu_id]:
def confirm_clearing():
yes_no_dlg = wx.MessageDialog(self.textCtrl_Editor,
"Are you sure you want to clear all of "
"the code?",
"Clear all?",
wx.YES_NO | wx.ICON_QUESTION)
result = yes_no_dlg.ShowModal() == wx.ID_YES
yes_no_dlg.Destroy()
return result

if confirm_clearing():
self.textCtrl_Editor.SetValue("")
cm.clear()

elif 'Copy' == const.EDITOR_ACTIONS[menu_id]:
self.textCtrl_Editor.Copy()

elif 'Select all' == const.EDITOR_ACTIONS[menu_id]:
self.textCtrl_Editor.SetFocus()
self.textCtrl_Editor.SelectAll()

elif 'Save code to file' == const.EDITOR_ACTIONS[menu_id]:
import os
dlg = wx.FileDialog(self, "Choose a file", '', '', "*.py",
wx.SAVE | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
with open(os.path.join(dlg.GetDirectory(),
dlg.GetFilename()), 'w') as out_file:
out_file.write("# automatically generated by SWAPY\n")
out_file.write(self.textCtrl_Editor.GetValue())
dlg.Destroy()

else:
raise RuntimeError("Unknown menu_id=%s for editor "
"menu" % menu_id)

def _init_windows_tree(self):
self.treeCtrl_ObjectsBrowser.DeleteAllItems()
item_data = wx.TreeItemData()
Expand All @@ -254,6 +364,7 @@ def _init_windows_tree(self):
#the_root = self.treeCtrl_ObjectsBrowser.GetRootItem()
#self.treeCtrl_ObjectsBrowser.Expand(self.treeCtrl_ObjectsBrowser.GetRootItem())


class prop_viewer_updater(object):
def __init__(self, listctrl):
self.listctrl = listctrl
Expand All @@ -274,7 +385,15 @@ def _update(self):
index = self.listctrl.InsertStringItem(0, 'Updating...')
self.listctrl.SetStringItem(index, 1, '')
global PROPERTIES
PROPERTIES = obj.GetProperties()
try:
PROPERTIES = obj.GetProperties()
except:
PROPERTIES = {}
dlg = wx.MessageDialog(self.listctrl, traceback.format_exc(5),
'Warning!', wx.OK | wx.ICON_WARNING)
dlg.ShowModal()
dlg.Destroy()

param_names = PROPERTIES.keys()
param_names.sort(key=lambda name: name.lower(), reverse=True)

Expand All @@ -285,7 +404,8 @@ def _update(self):
try:
p_values_str = str(PROPERTIES[p_name])
except exceptions.UnicodeEncodeError:
p_values_str = PROPERTIES[p_name].encode(locale.getpreferredencoding(), 'replace')
p_values_str = PROPERTIES[p_name].encode(
locale.getpreferredencoding(), 'replace')
index = self.listctrl.InsertStringItem(0, p_name_str)
self.listctrl.SetStringItem(index, 1, p_values_str)
self.queue = []
Expand All @@ -296,7 +416,8 @@ def _update(self):
#there is the newer object for properties view.
#Do not update listctrl
#run _update again



class tree_updater(object):
def __init__(self, treectrl):
self.treectrl = treectrl
Expand Down

0 comments on commit efab8ad

Please sign in to comment.