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 = {
'begin': '\x1b', #ESC
'begin': '\x1b', # ESC
'end': '\r'
}
}
MKPcommands = {
'handshake': {
@ -42,9 +42,9 @@ MKPcommands = {
},
'callPrintImage': {
'defPrefix': False,
'begin': '\x02', #STX
'end': '\x03', #ETX
'command': 'TZ{imagename};{printMode};{queueNo};{delay}\r{fieldContent}\r{counterStart}',
'begin': '\x02', # STX
'end': '\x03', # ETX
'command': 'TZ{imageName};{printMode};{queueNo};{delay}\r{fieldContent}\r{counterStart}',
'success': '\x06\x02([0-9])OK\x03',
'error': '([0-9])E([0-9])',
'args': ['imageName', 'printMode', 'fieldContent'],
@ -87,7 +87,7 @@ MKPcommands = {
'command': '*STD{dateTime};{offset}',
'success': 'Ok\r',
'error': 'Err\r',
'args': ['dateTime','offset']
'args': ['dateTime', 'offset']
},
'receiveDate': {
'defPrefix': True,
@ -113,4 +113,4 @@ MKPcommands = {
'success': True,
'error': None
},
}
}

View File

@ -13,17 +13,23 @@ import re
def cmdString(cmdName, **cmdArgs):
cmdDict = cmd.MKPcommands[cmdName]
mandatory_args = set(cmdDict['args'])
optional_args = set(cmdDict['optargs'])
if 'optArgs' in cmdDict and cmdDict['optArgs'] is not None:
optional_args = set(cmdDict['optArgs'])
else:
optional_args = set()
my_args = set(cmdArgs.keys())
if my_args.intersection(mandatory_args) != my_args:
if len(my_args) < len(mandatory_args) or my_args.intersection(mandatory_args) != my_args:
raise ValueError('Argomenti obbligatori errati o mancanti.')
if len(my_args-mandatory_args-optional_args) > 0:
raise Warning('Forniti argomenti sconosciuti!')
argDict = {**{a: cmdArgs[a] for a in mandatory_args}
** {a: cmdArgs[a] if a in my_args else '' for a in optional_args}
}
argDict = { ** { a: cmdArgs[a] for a in mandatory_args},
** { a: cmdArgs[a] if a in my_args else '' for a in optional_args} }
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)) \
+ re.sub(r';+', ';', cmdDict['command'].format(**argDict)) \
+ str(cmd.DEFprefix['end']
if cmdDict['defPrefix'] else cmdDict['end'])
@ -60,3 +66,6 @@ def checkError(cmdName, response):
if error_codes:
return error_codes.groups()
return None
testala=cmdString('callPrintImage',imageName='Pippus',printMode='10',fieldContent='qualcosa\rqualcosaltro\rciao')