You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
343 lines
17 KiB
343 lines
17 KiB
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
from pywinauto import win32defines, win32structures, win32functions
|
|
import pdb
|
|
import pywinauto
|
|
import json
|
|
import sys
|
|
import ctypes
|
|
import struct
|
|
import os
|
|
import select
|
|
import zlib
|
|
|
|
#Флаг оÑладки напÑÑмÑÑ (не вÑполнÑÑÑ ÑÑение бÑÑеÑа stdin)
|
|
mFlagIsDebug=False
|
|
|
|
#mPywinautoApplication=pywinauto.Application(backend="win32")
|
|
mPywinautoApplication=pywinauto.Application(backend="uia")
|
|
############################################
|
|
####ÐежпÑоÑеÑÑное взаимодейÑÑвие
|
|
############################################
|
|
#ProcessParentReadWaitString
|
|
def ProcessParentReadWaitString():
|
|
#ÐÑполниÑÑ ÑÑение ÑÑÑоки
|
|
#ctypes.windll.user32.MessageBoxW(0, "Hello", "Your title", 1)
|
|
lResult = sys.stdin.buffer.readline()
|
|
#ÐеÑнÑÑÑ Ð¿Ð¾ÑенÑиалÑнÑе \n
|
|
lResult = lResult.replace(b'{{n}}',b'\n')
|
|
lResult = zlib.decompress(lResult[0:-1])
|
|
lResult = lResult.decode("utf-8")
|
|
#ÐеÑнÑÑÑ ÑезÑлÑÑаÑ
|
|
return lResult
|
|
|
|
#ParentProcessWriteString
|
|
def ProcessParentWriteString(lString):
|
|
lByteString = zlib.compress(lString.encode("utf-8"))
|
|
#ÐÑполниÑÑ Ð¾ÑпÑÐ°Ð²ÐºÑ ÑÑÑоки в ÑодиÑелÑÑкий пÑоÑеÑÑ
|
|
#ÐеÑнÑÑÑ Ð¿Ð¾ÑенÑиалÑнÑе \n
|
|
lByteString = lByteString.replace(b'\n',b'{{n}}')
|
|
sys.stdout.buffer.write(lByteString+bytes("\n","utf-8"))
|
|
sys.stdout.flush();
|
|
return
|
|
#ProcessParentWriteObject
|
|
def ProcessParentWriteObject(inObject):
|
|
#ÐÑполниÑÑ Ð¾ÑпÑÐ°Ð²ÐºÑ ÑконвеÑÑиÑованного обÑекÑа в JSON
|
|
ProcessParentWriteString(json.dumps(inObject))
|
|
return
|
|
#ProcessParentReadWaitObject
|
|
def ProcessParentReadWaitObject():
|
|
#ÐÑполниÑÑ Ð¿Ð¾Ð»ÑÑение и ÑÐ°Ð·Ð±Ð¾Ñ Ð¾Ð±ÑекÑа
|
|
lResult=json.loads(ProcessParentReadWaitString());
|
|
return lResult;
|
|
|
|
##################################
|
|
###ÐеÑÐ¾Ð´Ñ Ð²Ð·Ð°Ð¸Ð¼Ð¾Ð´ÐµÐ¹ÑÑÐ²Ð¸Ñ Ñ GUI инÑеÑÑейÑом
|
|
##################################
|
|
#pywinauto
|
|
def GetControl(inControlSpecificationArray):
|
|
#ÐодгоÑовка взодного маÑÑива
|
|
inControlSpecificationArray=ElementSpecificationArraySearchPrepare(inControlSpecificationArray)
|
|
#ÐÑполниÑÑ Ð¸Ð´ÐµÐ½ÑиÑикаÑÐ¸Ñ Ð¾Ð±ÑекÑов, еÑли пеÑедан маÑÑив
|
|
lResultList=[];
|
|
if len(inControlSpecificationArray) > 0:
|
|
#ÐÑполниÑÑ Ð¿Ð¾Ð´ÐºÐ»ÑÑение к обÑекÑÑ
|
|
lRPAApplication = mPywinautoApplication
|
|
#ÐÑовеÑка ÑазÑÑдноÑÑи
|
|
|
|
lRPAApplication.connect(**inControlSpecificationArray[0])
|
|
lTempObject=lRPAApplication.window(**inControlSpecificationArray[0])
|
|
|
|
#ЦиклиÑеÑкое пÑоÑ
ождение к недÑам обÑекÑа
|
|
for lWindowSpecification in inControlSpecificationArray[1:]:
|
|
lTempObject=lTempObject.window(**lWindowSpecification)
|
|
return lTempObject
|
|
|
|
|
|
#ÐÑполниÑÑ Ð´ÐµÐ¹ÑÑвие над ÑлеменÑом
|
|
def ElementRunAction(inControlSpecificationArray,inActionName,inArgumentList=[],inkwArgumentObject={}):
|
|
#ÐпÑеделиÑÑ Ð¾Ð±ÑекÑ
|
|
lObject=GetControl(inControlSpecificationArray)
|
|
#ÐолÑÑиÑÑ Ð¼ÐµÑод Ð´Ð»Ñ Ð²Ñзова
|
|
lFunction = getattr(lObject.wrapper_object(), inActionName)
|
|
#ÐÑполниÑÑ Ð´ÐµÐ¹ÑÑвие
|
|
lFunction(*inArgumentList,**inkwArgumentObject)
|
|
return
|
|
|
|
def ElementGetInfo(inControlSpecificationArray):
|
|
#ÐодгоÑовка вÑ
одного маÑÑива
|
|
inControlSpecificationArray=ElementSpecificationArraySearchPrepare(inControlSpecificationArray)
|
|
#ÐÑполниÑÑ Ð¸Ð´ÐµÐ½ÑиÑикаÑÐ¸Ñ Ð¾Ð±ÑекÑов, еÑли пеÑедан маÑÑив
|
|
lResultList=[];
|
|
if len(inControlSpecificationArray) > 0:
|
|
#ÐÑполниÑÑ Ð¿Ð¾Ð´ÐºÐ»ÑÑение к обÑекÑÑ
|
|
lRPAApplication = mPywinautoApplication
|
|
#ÐÑовеÑка ÑазÑÑдноÑÑи
|
|
|
|
lRPAApplication.connect(**inControlSpecificationArray[0])
|
|
lTempObject=lRPAApplication.window(**inControlSpecificationArray[0])
|
|
|
|
#ЦиклиÑеÑкое пÑоÑ
ождение к недÑам обÑекÑа
|
|
for lWindowSpecification in inControlSpecificationArray[1:]:
|
|
lTempObject=lTempObject.window(**lWindowSpecification)
|
|
#ÐолÑÑиÑÑ Ð¸Ð½Ñо обÑекÑ
|
|
lTempObjectInfo = lTempObject.wrapper_object().element_info
|
|
#ÐобавиÑÑ Ð¸Ð½ÑоÑмаÑÐ¸Ñ Ð¾Ð± обнаÑÑженом обÑекÑе
|
|
lResultList.append(ElementInfoExportObject(lTempObjectInfo));
|
|
return lResultList
|
|
#debug
|
|
def ElementGetChildElementList(inControlSpecificationArray=[]):
|
|
#ÐодгоÑовка вÑ
одного маÑÑива
|
|
inControlSpecificationArray=ElementSpecificationArraySearchPrepare(inControlSpecificationArray)
|
|
#ÐÑполниÑÑ Ð¸Ð´ÐµÐ½ÑиÑикаÑÐ¸Ñ Ð¾Ð±ÑекÑов, еÑли пеÑедан маÑÑив
|
|
lResultList=[];
|
|
#ctypes.windll.user32.MessageBoxW(0, str(inControlSpecificationArray), "Your title", 1)
|
|
if len(inControlSpecificationArray) > 0:
|
|
#ÐÑполниÑÑ Ð¿Ð¾Ð´ÐºÐ»ÑÑение к обÑекÑÑ
|
|
lRPAApplication = mPywinautoApplication
|
|
lRPAApplication.connect(**inControlSpecificationArray[0])
|
|
lTempObject=lRPAApplication.window(**inControlSpecificationArray[0])
|
|
|
|
#ЦиклиÑеÑкое пÑоÑ
ождение к недÑам обÑекÑа
|
|
for lWindowSpecification in inControlSpecificationArray[1:]:
|
|
lTempObject=lTempObject.window(**lWindowSpecification)
|
|
#ÐолÑÑиÑÑ ÑпиÑок доÑеÑниÑ
обÑекÑов
|
|
lTempChildList = lTempObject.wrapper_object().children()
|
|
#ÐодгоÑовиÑÑ ÑезÑлÑÑиÑÑÑÑий обÑекÑ
|
|
for lChild in lTempChildList:
|
|
lTempObjectInfo=lChild.element_info
|
|
#ÐобавиÑÑ Ð¸Ð½ÑоÑмаÑÐ¸Ñ Ð¾Ð± обнаÑÑженом обÑекÑе
|
|
lResultList.append(ElementInfoExportObject(lTempObjectInfo));
|
|
else:
|
|
lResultList=GetRootElementList()
|
|
return lResultList
|
|
|
|
#ÐодгоÑовиÑÑ Ð¼Ð°ÑÑив Ð´Ð»Ñ Ð¾Ð±ÑаÑÑÐµÐ½Ð¸Ñ Ðº поиÑÐºÑ ÑлемеменÑов
|
|
def ElementSpecificationArraySearchPrepare(inControlSpecificationArray):
|
|
lResult=[]
|
|
#ЦиклиÑеÑкий обÑ
од
|
|
for lSpecificationItem in inControlSpecificationArray:
|
|
lSpecificationItemNew=lSpecificationItem.copy()
|
|
#ÐеÑÐµÐ±Ð¾Ñ Ð²ÑеÑ
ÑлеменÑов
|
|
for lItemKey,lItemValue in lSpecificationItem.items():
|
|
#Флаг ÑÐ´Ð°Ð»ÐµÐ½Ð¸Ñ Ð°ÑÑибÑÑа
|
|
lFlagRemoveAttribute=False
|
|
#############################
|
|
#ÐÑли ÑвлÑеÑÑÑ Ð²Ð»Ð¾Ð¶ÐµÐ½Ð½Ñм ÑловаÑем - ÑдалиÑÑ
|
|
if type(lItemValue) is dict:
|
|
lFlagRemoveAttribute=True
|
|
#ЯвлÑеÑÑÑ Ñипом None
|
|
if lItemValue is None:
|
|
lFlagRemoveAttribute=True
|
|
#ÐÑовеÑка допÑÑÑимого клÑÑевого Ñлова
|
|
if (
|
|
lItemKey == "class_name" or
|
|
lItemKey == "class_name_re" or
|
|
lItemKey == "parent" or
|
|
lItemKey == "process" or
|
|
lItemKey == "title" or
|
|
lItemKey == "title_re" or
|
|
lItemKey == "top_level_only" or
|
|
lItemKey == "visible_only" or
|
|
lItemKey == "enabled_only" or
|
|
lItemKey == "best_match" or
|
|
lItemKey == "handle" or
|
|
lItemKey == "ctrl_index" or
|
|
lItemKey == "found_index" or
|
|
lItemKey == "predicate_func" or
|
|
lItemKey == "active_only" or
|
|
lItemKey == "control_id" or
|
|
lItemKey == "control_type" or
|
|
lItemKey == "auto_id" or
|
|
lItemKey == "framework_id" or
|
|
lItemKey == "backend"):
|
|
True == True
|
|
else:
|
|
lFlagRemoveAttribute=True
|
|
|
|
#############################
|
|
#ÐонÑÑÑÑкÑÐ¸Ñ Ð¿Ð¾ ÑÐ´Ð°Ð»ÐµÐ½Ð¸Ñ ÐºÐ»ÑÑа из ÑловаÑÑ
|
|
if lFlagRemoveAttribute:
|
|
lSpecificationItemNew.pop(lItemKey)
|
|
#ÐобавиÑÑ ÑÑÑÐ¾ÐºÑ Ð² ÑезÑлÑÑиÑÑÑÑий маÑÑив
|
|
lResult.append(lSpecificationItemNew)
|
|
#ÐеÑнÑÑÑ ÑезÑлÑÑаÑ
|
|
return lResult
|
|
|
|
#ÐолÑÑиÑÑ Ð¾Ð±ÑÐµÐºÑ Ð¸Ð· аÑÑибÑÑов, коÑоÑÑе ÑдалоÑÑ Ð¿ÑоÑиÑаÑÑ
|
|
def ElementInfoExportObject(inElementInfo):
|
|
#ÐодгоÑовиÑÑ Ð²ÑÑ
однÑÑ ÑÑÑÑкÑÑÑÑ Ð´Ð°Ð½Ð½ÑÑ
|
|
lResult = {"name":None,"rich_text":None,"process_id":None,"handle":None,"class_name":None,"control_type":None,"control_id":None,"rectangle":{"left":None,"top":None,"right":None,"bottom":None}, 'runtime_id':None}
|
|
#ÐÑовеÑка name
|
|
try:
|
|
lResult['name']=inElementInfo.name
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка rich_text
|
|
try:
|
|
lResult['rich_text']=inElementInfo.rich_text
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка process_id
|
|
try:
|
|
lResult['process_id']=inElementInfo.process_id
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка handle
|
|
try:
|
|
lResult['handle']=inElementInfo.handle
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка class_name
|
|
try:
|
|
lResult['class_name']=inElementInfo.class_name
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка control_type
|
|
try:
|
|
lResult['control_type']=inElementInfo.control_type
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка control_id
|
|
try:
|
|
lResult['control_id']=inElementInfo.control_id
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка rectangle left
|
|
try:
|
|
lResult['rectangle']['left']=inElementInfo.rectangle.left
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка rectangle right
|
|
try:
|
|
lResult['rectangle']['right']=inElementInfo.rectangle.right
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка rectangle top
|
|
try:
|
|
lResult['rectangle']['top']=inElementInfo.rectangle.top
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка rectangle bottom
|
|
try:
|
|
lResult['rectangle']['bottom']=inElementInfo.rectangle.bottom
|
|
except Exception as e:
|
|
True == False
|
|
#ÐÑовеÑка runtime_id
|
|
try:
|
|
lResult['runtime_id']=inElementInfo.runtime_id
|
|
except Exception as e:
|
|
True == False
|
|
#ÐеÑнÑÑÑ ÑезÑлÑÑаÑ
|
|
return lResult
|
|
|
|
def GetRootElementList():
|
|
#ÐолÑÑиÑÑ ÑпиÑок обÑекÑов
|
|
lResultList=pywinauto.findwindows.find_elements()
|
|
lResultList2=[]
|
|
for lI in lResultList:
|
|
lTempObjectInfo=lI
|
|
lResultList2.append(ElementInfoExportObject(lI));
|
|
return lResultList2
|
|
def ElementDrawOutlineNew(inSpecificationArray):
|
|
draw_outline_new(GetControl(inSpecificationArray))
|
|
return
|
|
def draw_outline_new(lWrapperObject,colour='green',thickness=2,fill=win32defines.BS_NULL,rect=None):
|
|
"""
|
|
Draw an outline around the window.
|
|
* **colour** can be either an integer or one of 'red', 'green', 'blue'
|
|
(default 'green')
|
|
* **thickness** thickness of rectangle (default 2)
|
|
* **fill** how to fill in the rectangle (default BS_NULL)
|
|
* **rect** the coordinates of the rectangle to draw (defaults to
|
|
the rectangle of the control)
|
|
"""
|
|
#pdb.set_trace()
|
|
# don't draw if dialog is not visible
|
|
#if not lWrapperObject.is_visible():
|
|
# return
|
|
colours = {
|
|
"green": 0x00ff00,
|
|
"blue": 0xff0000,
|
|
"red": 0x0000ff,
|
|
}
|
|
# if it's a known colour
|
|
if colour in colours:
|
|
colour = colours[colour]
|
|
if rect is None:
|
|
rect = lWrapperObject.rectangle()
|
|
# create the pen(outline)
|
|
pen_handle = win32functions.CreatePen(
|
|
win32defines.PS_SOLID, thickness, colour)
|
|
# create the brush (inside)
|
|
brush = win32structures.LOGBRUSH()
|
|
brush.lbStyle = fill
|
|
brush.lbHatch = win32defines.HS_DIAGCROSS
|
|
brush_handle = win32functions.CreateBrushIndirect(ctypes.byref(brush))
|
|
# get the Device Context
|
|
dc = win32functions.CreateDC("DISPLAY", None, None, None )
|
|
# push our objects into it
|
|
win32functions.SelectObject(dc, brush_handle)
|
|
win32functions.SelectObject(dc, pen_handle)
|
|
# draw the rectangle to the DC
|
|
win32functions.Rectangle(
|
|
dc, rect.left, rect.top, rect.right, rect.bottom)
|
|
# Delete the brush and pen we created
|
|
win32functions.DeleteObject(brush_handle)
|
|
win32functions.DeleteObject(pen_handle)
|
|
# delete the Display context that we created
|
|
win32functions.DeleteDC(dc)
|
|
|
|
#run()
|
|
lText = "Bitness:" + str(struct.calcsize("P") * 8)
|
|
#for line in sys.stdin:
|
|
# lText=lText+line;
|
|
#ctypes.windll.user32.MessageBoxW(0, lText, "Your title", 1)
|
|
|
|
buffer = ""
|
|
lJSONInputString=""
|
|
#ÐÑполниÑÑ ÑÑение бÑÑеÑа, еÑли не оÑладка библиоÑеки
|
|
if not mFlagIsDebug:
|
|
#{'functionName':'', 'argsArray':[]}
|
|
while True:
|
|
try:
|
|
lJSONInput = ProcessParentReadWaitObject()
|
|
lJSONInputString=str(lJSONInput)
|
|
#{'outputObject':''}
|
|
#ÐÑполниÑÑ Ð²Ñзов ÑÑнкÑии
|
|
|
|
lResult=locals()[lJSONInput['functionName']](*lJSONInput['argsArray'])
|
|
lJSONInput['outputObject']=lResult
|
|
ProcessParentWriteObject(lJSONInput)
|
|
except Exception as e:
|
|
#ÐÑвод оÑибки в ÑодиÑелÑÑкий поÑок
|
|
ProcessParentWriteObject({'Error':str(e), 'ArgObject':str(lJSONInputString)})
|
|
else:
|
|
print('Debug mode is turned on!')
|
|
|
|
#if __name__ == '__main__':
|
|
# if len(sys.argv) > 1:
|
|
# lFunctionArgs = sys.argv[2:]
|
|
# print(locals()[sys.argv[1]](*lFunctionArgs))
|
|
|
|
|