Bugfix quasi terminato

master
Guido Longoni 2019-09-27 18:55:52 +02:00
parent 4df4253e61
commit 92bc86c48b
2 changed files with 75 additions and 66 deletions

View File

@ -5,9 +5,9 @@ Created on 27 set 2019
''' '''
DEFprefix = { DEFprefix = {
'begin': '\x1b', #ESC 'begin': '\x1b', # ESC
'end': '\r' 'end': '\r'
} }
MKPcommands = { MKPcommands = {
'handshake': { 'handshake': {
@ -15,102 +15,102 @@ MKPcommands = {
'command': '*', 'command': '*',
'success': 'Ok\r', 'success': 'Ok\r',
'error': None 'error': None
}, },
'reset': { 'reset': {
'defPrefix': True, 'defPrefix': True,
'command': 'RST', 'command': 'RST',
'success': 'Ok\r', 'success': 'Ok\r',
'error': None, 'error': None,
}, },
'deleteMemory': { 'deleteMemory': {
'defPrefix': True, 'defPrefix': True,
'command': 'C', 'command': 'C',
'success': None, 'success': None,
'error': None 'error': None
}, },
'printPause': { 'printPause': {
'defPrefix': True, 'defPrefix': True,
'command': 'C0', 'command': 'C0',
'success': 'Ok\r', 'success': 'Ok\r',
'error': None, 'error': None,
}, },
'printStart': { 'printStart': {
'defPrefix': True, 'defPrefix': True,
'command': 'C1', 'command': 'C1',
'success': 'Ok\r', 'success': 'Ok\r',
'error': None, 'error': None,
}, },
'callPrintImage': { 'callPrintImage': {
'defPrefix': False, 'defPrefix': False,
'begin': '\x02', #STX 'begin': '\x02', # STX
'end': '\x03', #ETX 'end': '\x03', # ETX
'command': 'TZ{imagename};{printMode};{queueNo};{delay}\r{fieldContent}\r{counterStart}', 'command': 'TZ{imageName};{printMode};{queueNo};{delay}\r{fieldContent}\r{counterStart}',
'success': '\x06\x02([0-9])OK\x03', 'success': '\x06\x02([0-9])OK\x03',
'error': '([0-9])E([0-9])', 'error': '([0-9])E([0-9])',
'args': ['imageName', 'printMode', 'fieldContent'], 'args': ['imageName', 'printMode', 'fieldContent'],
'optArgs': ['queueNo', 'delay', 'counterStart'] 'optArgs': ['queueNo', 'delay', 'counterStart']
}, },
'sendImage': { 'sendImage': {
'defPrefix': True, 'defPrefix': True,
'command': 'EW----;{imageName}', 'command': 'EW----;{imageName}',
'success': None, 'success': None,
'error': None, 'error': None,
'args': ['imageName'] 'args': ['imageName']
}, },
'sendImageEnd': { 'sendImageEnd': {
'defPrefix': True, 'defPrefix': True,
'command': 'EX----;', 'command': 'EX----;',
'success': 'OK\r', 'success': 'OK\r',
'error': None, 'error': None,
}, },
'receiveImage': { 'receiveImage': {
'defPrefix': True, 'defPrefix': True,
'command': 'EL----;', 'command': 'EL----;',
'success': True, 'success': True,
'error': None 'error': None
}, },
'receiveDir': { 'receiveDir': {
'defPrefix': True, 'defPrefix': True,
'command': 'ED----;', 'command': 'ED----;',
'success': True, 'success': True,
'error': None 'error': None
}, },
'deleteImage': { 'deleteImage': {
'defPrefix': True, 'defPrefix': True,
'command': 'EQ----;{imageName}', 'command': 'EQ----;{imageName}',
'success': 'OK\r', 'success': 'OK\r',
'error': 'Err\r', 'error': 'Err\r',
'args': ['imageName'] 'args': ['imageName']
}, },
'sendDate': { 'sendDate': {
'defPrefix': True, 'defPrefix': True,
'command': '*STD{dateTime};{offset}', 'command': '*STD{dateTime};{offset}',
'success': 'Ok\r', 'success': 'Ok\r',
'error': 'Err\r', 'error': 'Err\r',
'args': ['dateTime','offset'] 'args': ['dateTime', 'offset']
}, },
'receiveDate': { 'receiveDate': {
'defPrefix': True, 'defPrefix': True,
'command': '*GSTD', 'command': '*GSTD',
'success': True, 'success': True,
'error': None, 'error': None,
}, },
'status': { 'status': {
'defPrefix': True, 'defPrefix': True,
'command': 'SV', 'command': 'SV',
'success': True, 'success': True,
'error': None 'error': None
}, },
'status1': { 'status1': {
'defPrefix': True, 'defPrefix': True,
'command': 'S1', 'command': 'S1',
'success': True, 'success': True,
'error': None 'error': None
}, },
'status2': { 'status2': {
'defPrefix': True, 'defPrefix': True,
'command': 'S2', 'command': 'S2',
'success': True, 'success': True,
'error': None 'error': None
}, },
} }

View File

@ -11,52 +11,61 @@ import re
def cmdString(cmdName, **cmdArgs): def cmdString(cmdName, **cmdArgs):
cmdDict = cmd.MKPcommands[cmdName] cmdDict = cmd.MKPcommands[cmdName]
mandatory_args = set(cmdDict['args']) mandatory_args = set(cmdDict['args'])
optional_args = set(cmdDict['optargs']) if 'optArgs' in cmdDict and cmdDict['optArgs'] is not None:
my_args = set(cmdArgs.keys()) optional_args = set(cmdDict['optArgs'])
if my_args.intersection(mandatory_args) != my_args: else:
raise ValueError('Argomenti obbligatori errati o mancanti.') optional_args = set()
if len(my_args-mandatory_args-optional_args) > 0: my_args = set(cmdArgs.keys())
raise Warning('Forniti argomenti sconosciuti!') if len(my_args) < len(mandatory_args) or my_args.intersection(mandatory_args) != my_args:
argDict = {**{a: cmdArgs[a] for a in mandatory_args} raise ValueError('Argomenti obbligatori errati o mancanti.')
** {a: cmdArgs[a] if a in my_args else '' for a in optional_args} if len(my_args-mandatory_args-optional_args) > 0:
} raise Warning('Forniti argomenti sconosciuti!')
return str(cmd.DEFprefix['begin'] if cmdDict['defPrefix'] else cmdDict['begin']) \ argDict = { ** { a: cmdArgs[a] for a in mandatory_args},
+ re.sub(r';+', ';', cmdDict['command'].format(argDict)) \ ** { a: cmdArgs[a] if a in my_args else '' for a in optional_args} }
+ str(cmd.DEFprefix['end']
if cmdDict['defPrefix'] else cmdDict['end']) arg_groups = [i.group(1) for i in re.finditer(r'[^;]((\{[^}]*\};)+\{[^}]*\})[^;]',cmd.MKPcommands[cmdName]['command'])]
replacements = [(g,';'.join([a.format(**argDict) for a in g.split(';') if len(a.format(**argDict)) > 0])) for g in arg_groups]
return str(cmd.DEFprefix['begin'] if cmdDict['defPrefix'] else cmdDict['begin']) \
+ re.sub(r';+', ';', cmdDict['command'].format(**argDict)) \
+ str(cmd.DEFprefix['end']
if cmdDict['defPrefix'] else cmdDict['end'])
def checkResponse(cmdName, response): def checkResponse(cmdName, response):
sucKey = 'success' sucKey = 'success'
cmdDict = cmd.MKPcommands[cmdName] cmdDict = cmd.MKPcommands[cmdName]
if sucKey in cmdDict and cmdDict[sucKey] is not None: if sucKey in cmdDict and cmdDict[sucKey] is not None:
if cmdDict[sucKey] == True: if cmdDict[sucKey] == True:
error = checkError(cmdName, response) error = checkError(cmdName, response)
if error is None: if error is None:
return None return None
else: else:
return error return error
if re.match(cmdDict[sucKey], response, re.I): if re.match(cmdDict[sucKey], response, re.I):
return True return True
error = checkError(cmdName, response) error = checkError(cmdName, response)
if error is None: if error is None:
raise NotImplementedError( raise NotImplementedError(
'Ricevuta risposta sconosciuta: {}'.format(response)) 'Ricevuta risposta sconosciuta: {}'.format(response))
else: else:
return error return error
else: else:
error = checkError(cmdName, response) error = checkError(cmdName, response)
return error return error
def checkError(cmdName, response): def checkError(cmdName, response):
errKey = 'error' errKey = 'error'
cmdDict = cmd.MKPcommands[cmdName] cmdDict = cmd.MKPcommands[cmdName]
if errKey in cmdDict and cmdDict[errKey] is not None: if errKey in cmdDict and cmdDict[errKey] is not None:
return None return None
error_codes = re.match(cmdDict[errKey], response, re.I) error_codes = re.match(cmdDict[errKey], response, re.I)
if error_codes: if error_codes:
return error_codes.groups() return error_codes.groups()
return None return None
testala=cmdString('callPrintImage',imageName='Pippus',printMode='10',fieldContent='qualcosa\rqualcosaltro\rciao')