def _canonicalfields(self,noderecord,structure_record,headerrecordnumber): ''' For fields: check M/C; format the fields. Field are not sorted here (a dict can not be sorted) ''' for grammarfield in structure_record[FIELDS]: if grammarfield[ISFIELD]: #if field (no composite) value = noderecord.get(grammarfield[ID],'') if not value: if grammarfield[MANDATORY] == 'M': raise botslib.MessageError(_(u'Record "$mpath" field "$field" is mandatory.'),mpath=structure_record[MPATH],field=grammarfield[ID]) elif not grammarfield[MINLENGTH]: #if minlength=0 continue noderecord[grammarfield[ID]] = self._formatfield(value,grammarfield,structure_record) else: #if composite compositefilled = False for grammarsubfield in grammarfield[SUBFIELDS]: #loop subfields to see if one of them is there if noderecord.get(grammarsubfield[ID],''): compositefilled = True if not compositefilled: if grammarfield[MANDATORY]=='M': raise botslib.MessageError(_(u'Record "$mpath" composite "$field" is mandatory.'),mpath=structure_record[MPATH],field=grammarfield[ID]) continue for grammarsubfield in grammarfield[SUBFIELDS]: #loop subfields value = noderecord.get(grammarsubfield[ID],'') if not value: if grammarsubfield[MANDATORY]=='M': raise botslib.MessageError(_(u'Record "$mpath" subfield "$field" is mandatory: "$record".'),mpath=structure_record[MPATH],field=grammarsubfield[ID],record=noderecord) else: continue noderecord[grammarsubfield[ID]] = self._formatfield(value,grammarsubfield,structure_record)
def _canonicaltree(self, node, structure, headerrecordnumber=0): ''' For nodes: check min and max occurence; sort the records conform grammar ''' sortednodelist = [] self._canonicalfields( node.record, structure, headerrecordnumber) #handle fields of this record if LEVEL in structure: for structure_record in structure[ LEVEL]: #for structure_record of this level in grammar count = 0 #count number of occurences of record for childnode in node.children: #for every node in mpathtree; SPEED: delete nodes from list when found if childnode.record['BOTSID'] != structure_record[ ID]: #if it is not the right NODE": continue count += 1 self._canonicaltree( childnode, structure_record, self.recordnumber) #use rest of index in deeper level sortednodelist.append(childnode) if structure_record[MIN] > count: raise botslib.MessageError( _(u'Record "$mpath" mandatory but not present.'), mpath=structure_record[MPATH]) if structure_record[MAX] < count: raise botslib.MessageError( _(u'Record "$mpath" occurs to often ($count times).'), mpath=structure_record[MPATH], count=count) node.children = sortednodelist if hasattr(self, 'get_queries_from_edi'): self.get_queries_from_edi(node, structure)
def _canonicalfields(self, noderecord, structure_record, headerrecordnumber): ''' For fields: check M/C; format the fields. Fields are not sorted (a dict can not be sorted). Fields are never added. ''' for grammarfield in structure_record[FIELDS]: if grammarfield[ISFIELD]: #if field (no composite) value = noderecord.get(grammarfield[ID]) #~ print '(message)field',noderecord,grammarfield if not value: #~ print 'field',grammarfield[ID], 'has no value' if grammarfield[MANDATORY] == 'M': raise botslib.MessageError( _(u'Record "$mpath" field "$field" is mandatory.'), mpath=structure_record[MPATH], field=grammarfield[ID]) continue #~ print 'field',grammarfield[ID], 'value', value noderecord[grammarfield[ID]] = self._formatfield( value, grammarfield, structure_record) else: #if composite for grammarsubfield in grammarfield[ SUBFIELDS]: #loop subfields to see if data in composite if noderecord.get(grammarsubfield[ID]): break #composite has data. else: #composite has no data if grammarfield[MANDATORY] == 'M': raise botslib.MessageError(_( u'Record "$mpath" composite "$field" is mandatory.' ), mpath=structure_record[ MPATH], field=grammarfield[ID]) continue #there is data in the composite! for grammarsubfield in grammarfield[ SUBFIELDS]: #loop subfields value = noderecord.get(grammarsubfield[ID]) if not value: if grammarsubfield[MANDATORY] == 'M': raise botslib.MessageError( _(u'Record "$mpath" subfield "$field" is mandatory: "$record".' ), mpath=structure_record[MPATH], field=grammarsubfield[ID], record=noderecord) continue noderecord[grammarsubfield[ID]] = self._formatfield( value, grammarsubfield, structure_record)
def _checkfields(self, record, structure_record): ''' checks for every field in record if field exists in structure_record (from grammar). ''' deletelist = [] for field in record.keys( ): #all fields in record should exist in structure_record for grammarfield in structure_record[FIELDS]: if grammarfield[ISFIELD]: #if field (no composite) if field == grammarfield[ID]: break else: #if composite for grammarsubfield in grammarfield[ SUBFIELDS]: #loop subfields if field == grammarsubfield[ID]: break else: continue break else: if self.ta_info['checkunknownentities']: raise botslib.MessageError( _(u'Record: "$mpath" field "$field" does not exist.'), field=field, mpath=structure_record[MPATH]) deletelist.append(field) for field in deletelist: del record[field]
def checkforerrorlist(self): ''' examine the message-object for errors; ''' if self.errorfatal: #for fatal errors: (try to) get information like partners for edi file self.try_to_retrieve_info() if self.errorlist: raise botslib.MessageError(u''.join(self.errorlist))
def add2errorlist(self, errortxt): self.errorlist.append(errortxt) if len(self.errorlist) >= botsglobal.ini.getint( 'settings', 'max_number_errors', 10): raise botslib.MessageError(_( u'Found at least $max_number_errors errors in message:\n$errorlist' ), max_number_errors=len(self.errorlist), errorlist=''.join(self.errorlist))
def _checktree(self,tree,structure): ''' checks tree with table: - all records should be in table at the right place in hierarchy - for each record, all fields should be in grammar This function checks the root of grammar-structure with root of node tree ''' if tree.record['BOTSID'] == structure[ID]: #check tree recursively with structure self._checktreecore(tree,structure) else: raise botslib.MessageError(_(u'Grammar "$grammar" has (root)record "$grammarroot"; found "$root".'),root=tree.record['BOTSID'],grammarroot=structure[ID],grammar=self.defmessage.grammarname)
def _checktreecore(self,node,structure): ''' recursive ''' deletelist=[] self._checkfields(node.record,structure) if node.children and not LEVEL in structure: if self.ta_info['checkunknownentities']: raise botslib.MessageError(_(u'Record "$record" in message has children, but grammar "$grammar" not. Found "$xx".'),record=node.record['BOTSID'],grammar=self.defmessage.grammarname,xx=node.children[0].record['BOTSID']) node.children=[] return for childnode in node.children: #for every node: for structure_record in structure[LEVEL]: #search in grammar-records if childnode.record['BOTSID'] == structure_record[ID]: #if found right structure_record #check children recursive self._checktreecore(childnode,structure_record) break #check next mpathnode else: #checked all structure_record in grammar, but nothing found if self.ta_info['checkunknownentities']: raise botslib.MessageError(_(u'Record "$record" in message not in structure of grammar "$grammar". Whole record: "$content".'),record=childnode.record['BOTSID'],grammar=self.defmessage.grammarname,content=childnode.record) deletelist.append(childnode) for child in deletelist: node.children.remove(child)
def _checkonemessage(self, node_instance, grammar, subtranslation): structure = grammar.structure if not node_instance.record['BOTSID'] == structure[0][ID]: raise botslib.MessageError(_( u'Grammar "$grammar" has (root)record "$grammarroot"; found "$root".' ), root=node_instance.record['BOTSID'], grammarroot=structure[0][ID], grammar=grammar.grammarname) self._checkifrecordsingrammar(node_instance, structure[0], grammar.grammarname) self._canonicaltree(node_instance, structure[0]) if not subtranslation and botsglobal.ini.getboolean( 'settings', 'readrecorddebug', False ): #should the content of the message (the records read) be logged. self._logmessagecontent(node_instance)
def checkmessage(self, node_instance, grammar, subtranslation=False): ''' The node tree is check, sorted, fields are formatted etc. For checking: translation & subtranslation parameter subtranslation only used for reporting ''' #checks the root of grammar-structure with root of node tree: #check message against grammar (so far only minimal tests have been done during processing) #some different cases: #- empy root.record, root.children filled: # - edifact, x12, tradacoms: each child is an envelope. Check each envelope. (use mailbag to have one UNB per node-tree here) # - csv nobotsid: each child is a record. Check all records in one check # - xml, json: # root.record filled, root.children filled: outgoing messages. #~ self.root.display() #show tree of nodes (for protocol debugging) if node_instance.record: #root record contains information; write whole tree in one time self._checkonemessage(node_instance, grammar, subtranslation) else: for childnode in node_instance.children: self._checkonemessage(childnode, grammar, subtranslation) if self.errorlist and not subtranslation: raise botslib.MessageError(_(u'$errorlist'), errorlist=''.join(self.errorlist))