最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[tmp] python: parse command

tmp_todo crifan 961浏览 0评论

# FileName: CommandParse.py
# Function: Parse input string of Menu command

“””
the input Menu Command Grammar should be:
 <“SYN”> <“M”|”Y”>  <“CR”> <Tag> ((<SubTag> (<Data> | <“?”|”^”|”*”>)) | <“?”|”^”|”*”>) { “,” <SubTag> (<Data> | <“?”|”^”|”*”>) }
        { “;” <Tag> ((<SubTag> (<Data> | <“?”|”^”|”*”>)) | <“?”|”^”|”*”>) { “,” <SubTag> (<Data> | <“?”|”^”|”*”>) } }
 < ”.” | ”@” | ”!”>
“””
import sys

terminatorList = [“.”, “!”, “@”, “&”]
prefixList = [“x16Mx0D”, “x16Yx0D”]
# should add all valid main tag here
mainTagList = [“232”, “ALL”, “BK2”, “DEF”, “DFM”, “DLY”, “KBD”, “PRE”, “SUF”, “TRM”, “UPA”, “UPE”]

minPrefixLen = 3
minTerminatorLen = 1

# test case
menuCmd_1 = “x16Mx0D232BAD7,WRD2,CTS4,PKT2,DEL3100,ACK1,DLK5100.” #sub tag only
menuCmd_2 = “x16Mx0D232?,WRD2,CTS0,XON1,ACK0;DLYCHR0,FNC0,MSG0,CRX0!” # query in main tag
menuCmd_3 = “x16Yx0DUPA?;TRM*;232^.” # query main tag only
menuCmd_4 = “x16Yx0D232BAD9,WRD?,CTS^,XON*,ACK0;DLYCHR0,FNC^,MSG*,CRX0.” # query only in sub tag
menuCmd_5 = “x16Yx0D232^,WRD*,CTS^,XON0,ACK0;DLY*,FNC0,MSG0,CRX?.” # query in both main tag and sub tag
menuCmd_6 = “x16Mx0D232BAD?,WRD2,CTS^,XON0,ACK0;DLYCHR0,FNC0,MSG0,CRX0!”
menuCmd_7 = “x16Yx0DTRM2320;232BAD2;232CTS1;232WRD14;SUFBK2990A!” # every single command
menuCmd_8 = “x16Mx0DDEFALT.”
menuCmd_9 = “x16Mx0DSUFCA2;PRECA2;DFMCA3,_EN2.”
menuCmd_10 = “x16Mx0DPRECA2;SUFCA2,BK2990D,_EN1;KBDCAS0,ALT0,DLY8;DLYCHR0,MSG0,FNC0.”
menuCmd_11 = “x16Mx0DALLENA0;UPAENA1,CKX1,NSX1;UPEEN01,CKX1;TRM2320;232BAD5,CTS1,WRD2.”
menuCmd_12 = “x16M.” # invalid command: too short
menuCmd_13 = “x16Mx0DSUFCA2;PRECA2;DFMCA3,_EN2#” #invalid terminator
menuCmd_14 = “x16MSUFCA2;PRECA2;DFMCA3,_EN2.” #invalid terminator

#menuCmd = str(input(“Please Input Menu Command String:”))
menuCmd = menuCmd_3

print(“The input menu command string is:n”, menuCmd)
cmdLen = len(menuCmd)
if cmdLen < (minPrefixLen + minTerminatorLen) :
    print (“Invalid Menu Command len=”, cmdLen, “, is too short !!!”)
    sys.exit(“Invalid Menu Command for length is too short”)

# 1. check validation of prefix
prefix = menuCmd[0:3]
if (prefix in prefixList):
    print(“Prefix=”, prefix, “,is valid”)
else:
    print(“Prefix=”, prefix, “, is invalid !!!”)
    sys.exit(“Commmand Prefix is invalid”)

# 2. check validation of terminator
terminator = menuCmd[cmdLen-1]
if (terminator in terminatorList):
    print(“Terminator=”, terminator, “, is valid”)
else:
    print(“Terminator=”, terminator,”, is invalid !!!”)
    sys.exit(“Command terminator is invalid”)

menuCmd = menuCmd[3:] #remove the prefix
menuCmd = menuCmd[:-1] # remove terminator

print(“After delete prefix and terminator, the menu command string become:n”, menuCmd)

print(“——Following is every extracted sigle menu command——“)
mainFieldList = menuCmd.split(“;”)
# 3. process each main field, seperator by ‘;’
for singleMainField in mainFieldList:
    #  3.1 get each sub field
    subFieldList = singleMainField.split(“,”)
    firstSubField = subFieldList[0]
    curMainTag = firstSubField[0:3]
    if curMainTag in mainTagList:
        # 3.2 process each sub field, seperator by ‘,’
        print(subFieldList[0])
        for singleSubField in subFieldList[1:]:
            curSingleCommand = curMainTag + singleSubField
            # for actual code is so complex, so here not add code to check the validation of sub tag
            # only just print the valid menu command
            print(curSingleCommand)
            # in actual code, should add code for processing each menu command here
    else:
        print(“Inliad Main Tag=”, curMainTag)
        continue # goto process next

转载请注明:在路上 » [tmp] python: parse command

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
52 queries in 0.098 seconds, using 18.06MB memory