设计模式二十解释器模式:与之前自己开发的wechat robot 解析执行动作实现类似

189 阅读6分钟

image.png

按照概念的理解,我曾经实现过的 一套自动控制手机系统(可以通过录制或自己手动编辑脚本控制手机自动化做一些事情)里面有对不同配置文件的解释engine, 可以理解为解释模式吧。这里不做赘述,后面开放 这个 手机的 robot 代码 在gitee上。

大体的被解释文件如下图所示, 其中例如第1,2,3 比较短的item , 系统上层有默认一些设置,如果需要覆盖,在本文件里面 加入一些信息,到执行时刻,解析器会重新整理分析哪些是最终用户想要是配置。

下面的被解析文件是,启动微信 自动打开里面的打卡小程序,提供体温打卡服务,避免忘记给孩子打卡。

image.png

下面解析文件模版的意图是 打开美团选择城市和关键字,收集里面商家的店铺名称 电话等并保存到文件,里面每行的的序号解析的时候是忽略的,最终是按照解析器自己内部编制的序号

1.启动美团

事后计算:delay*1*compareImageSameJump*

2.返回上级

3.放大镜搜索坐标

之前界面:美团启动后

期待界面:美团地址选择后

事前计算:放大镜搜索

事前结果:posx-130,posy*0.7

动作执行:touchTap

事后计算:None

事后结果:None

出错状态转移码:+999

7.设置粘贴剪贴板本步骤没有界面变化

动作执行:setCliper*[1]

3.点击选择地区

之前界面:美团地址选择后

期待界面:美团点击搜索框长按后

事前计算:城市

事前结果:posx,posy*0.8

动作执行:touchLongpress

事后计算:None

事后结果:None

出错状态转移码:None

6.长按输入焦点调用起粘贴菜单

3.粘贴地址

之前界面:粘贴地址

期待界面:粘贴地址

事前计算:Local粘贴

事前结果:posx,posy*0.8

动作执行:touchTap

事后计算:None

事后结果:None

出错状态转移码:+999

3.美团选择地址

之前界面:选择地址

期待界面:选择地址

事前计算:地址选择

事前结果:posx,posy+60*0.8

动作执行:touchTap

事后计算:None

事后结果:None

出错状态转移码:None

3.放大镜搜索坐标

之前界面:美团首页

期待界面:美团搜索框点击后界面

事前计算:放大镜搜索

事前结果:posx+100,posy*0.8

动作执行:touchTap

事后计算:None

事后结果:None

出错状态转移码:+999

7.设置粘贴剪贴板本步骤没有界面变化

动作执行:setCliper*[2]

3.长按方便输入

之前界面:长按前

期待界面:长按后

事前计算:长按方便输入

事前结果:posx+100,posy*0.8

动作执行:touchLongpress

事后计算:None

事后结果:None

出错状态转移码:+999

3.粘贴关键字

之前界面:粘贴地址前

期待界面:粘贴地址后

事前计算:Local粘贴

事前结果:posx,posy*

动作执行:touchTap

事后计算:None

事后结果:None

出错状态转移码:+999

3.搜索关键字内容的点击

之前界面:点击搜索前

期待界面:点击搜索后

事前计算:搜索

事前结果:posx,posy*0.8

动作执行:touchTap

事后计算:delay*3*compareImageNotSameJump

事后结果:None

出错状态转移码:+1



3.打开一个店铺内容

之前界面:打开一个

期待界面:打开一个

事前计算:Local分*Local[2](2)

事前结果:posx,posy*

动作执行:touchTap

事后计算:delay*3*compareImageSameJump

事后结果:None

出错状态转移码:+4

3.点击分享按钮

之前界面:当前店铺详情

期待界面:当前店铺详情点击分享后

事前计算:点击分享按钮

事前结果:posx,posy*0.8

动作执行:touchTap*NoFoundNoAction

事后计算:None

事后结果:None

出错状态转移码:None

3.美团点击复制链接

之前界面:当前店铺详情点击分享后

期待界面:当前店铺详情点击复制链接后

事前计算:Local复制链接

事前结果:posx,posy*

动作执行:touchTap*NoFoundNoAction

事后计算:out*descSave*friendInfo/美团/[1]-[2]/*temp/美团店铺链接信息.txt

事后结果:./temp/美团店铺链接信息.txt

出错状态转移码:None

2.返回上级

3.美团列表界面滑动

之前界面:美团列表界面

期待界面:美团列表界面

事前计算:None

事前结果:None

动作执行:None

事后计算:out*swipeAndJustOver*temp/-[2]-.txt*temp/curl.txt*compareImageNotSameJump

事后结果:None

出错状态转移码:-4

3.ending〈

之前界面:ending

期待界面:ending

事前计算:Local结束了

事前结果:posx,posy*

动作执行:NoFoundNoAction

事后计算:None

事后结果:None

出错状态转移码:None

部分实现代码如下

#!/usr/bin/env python

# coding: utf-8

import os

import sys

from task.taskItem import *

class taskList:

    itemList = []

    currentPos = 0

    totalTaskItem = 0

    def __init__(self):

        pass

    def emptyItem(self):

        self.itemList.clear()

    def __init__(self,taskFile):

        nid = 0

        with open(taskFile, "rb") as f:

            for line in f:

                line = bytes.decode(line).strip()

                #split by .  get the title of taskitem

                if "." in line and not ':' in line:

                    titleInfo = line.split('.',1)

                    # if get the task title  create an item    a = taskItem()

                    # tasklist add title  set  currentParserNumber + 1

                    nid = nid+1

                    aTaskItem = taskItem(nid)

                    aTaskItem.taskItemName = titleInfo[1]

                    self.addItem(aTaskItem)

                # if not get the task title then  paser split by :

                if ":" in line:

                    propInfo = line.split(":",1)

                    if propInfo[0].strip() == "之前界面":

                        self.itemList[self.currentPos-1].currentScreen = propInfo[1]

                    if propInfo[0].strip() == "期待界面":

                        self.itemList[self.currentPos - 1].exceptScreen = propInfo[1]

                    if propInfo[0].strip() == "事前计算":

                        self.itemList[self.currentPos - 1].beforeCalcCommond = propInfo[1]

                    if propInfo[0].strip() == "事前结果":

                        self.itemList[self.currentPos - 1].beforeCalcResult = propInfo[1]

                    if propInfo[0].strip() == "事后计算":

                        self.itemList[self.currentPos - 1].afterCalcCommond = propInfo[1]

                    if propInfo[0].strip() == "事后结果":

                        self.itemList[self.currentPos - 1].afterCalcResult = propInfo[1]

                    if propInfo[0].strip() == "动作执行":

                        self.itemList[self.currentPos - 1].actionCommand = propInfo[1]

                    if propInfo[0].strip() == "出错状态转移码":

                        self.itemList[self.currentPos - 1].errorStatusNo = propInfo[1]



        self.totalTaskItem = self.currentPos

        print(self.totalTaskItem)

        self.currentPos = 0



        for i in range(self.currentPos,self.totalTaskItem):

            itemCommonPath ="./shouji/"+getHandsetType()+"/taskCommon/"+self.itemList[i].taskItemName+"/config.ini"

            if os.path.exists(itemCommonPath):

                with open(itemCommonPath, "rb") as f:

                    for line in f:

                        line = bytes.decode(line).strip()

                        if ":" in line:

                            propInfo = line.split(":", 1)

                            if propInfo[0].strip() == "期待界面" and self.itemList[i].exceptScreen =="":

                                self.itemList[i].exceptScreen = propInfo[1]

                            if propInfo[0].strip() == "之前界面" and self.itemList[i].currentScreen == "":

                                self.itemList[i].currentScreen = propInfo[1]

                            if propInfo[0].strip() == "事前计算" and self.itemList[i].beforeCalcCommond=="":

                                self.itemList[i].beforeCalcCommond = propInfo[1]

                            if propInfo[0].strip() == "事前结果" and self.itemList[i].beforeCalcResult == "":

                                self.itemList[i].beforeCalcResult = propInfo[1]

                            if propInfo[0].strip() == "事后计算" and self.itemList[i].afterCalcCommond=="":

                                self.itemList[i].afterCalcCommond = propInfo[1]

                            if propInfo[0].strip() == "事后结果" and self.itemList[i].afterCalcResult == "":

                                self.itemList[i].afterCalcResult = propInfo[1]

                            if propInfo[0].strip() == "动作执行" and self.itemList[i].actionCommand =="":

                                self.itemList[i].actionCommand = propInfo[1]

                            if propInfo[0].strip() == "出错状态转移码" and self.itemList[i].errorStatusNo == "":

                                self.itemList[i].errorStatusNo = propInfo[1]

        if 1:# display debug info to debug console

             print("print the taskinfo"+taskFile+"****************************")

             for i in range(self.currentPos,self.totalTaskItem):

                print(i+1)

                print(self.itemList[i].taskItemName)

                print("之前界面:"+self.itemList[i].currentScreen)

                print("期待界面:"+self.itemList[i].exceptScreen)

                print("事前计算:"+self.itemList[i].beforeCalcCommond)

                print("事前结果:"+self.itemList[i].beforeCalcResult)

                print("动作执行:"+self.itemList[i].actionCommand)

                print("事后计算:"+self.itemList[i].afterCalcCommond)

                print("事后结果:"+self.itemList[i].afterCalcResult)

                print("出错状态转移码:"+self.itemList[i].errorStatusNo)

             print("print the taskinfo"+taskFile+"****************************")

\