示例#1
0
def filer(request,*kw,**kwargs):
    ''' handles bots file viewer. Only files in data dir of Bots are displayed.'''
    nosuchfile = _(u'No such file.')
    if request.method == 'GET':
        try:
            idta = request.GET['idta']
        except:
            return  django.shortcuts.render_to_response('bots/filer.html', {'error_content': nosuchfile},context_instance=django.template.RequestContext(request))
        if idta == 0: #for the 'starred' file names (eg multiple output)
            return  django.shortcuts.render_to_response('bots/filer.html', {'error_content': nosuchfile},context_instance=django.template.RequestContext(request))
        try:
            currentta = list(models.ta.objects.filter(idta=idta))[0]
            if request.GET['action']=='downl':
                response = django.http.HttpResponse(mimetype=currentta.contenttype)
                if  currentta.contenttype == 'text/html':
                    dispositiontype = 'inline'
                else:
                    dispositiontype = 'attachment'
                response['Content-Disposition'] = dispositiontype + '; filename=' + currentta.filename + '.txt'
                #~ response['Content-Length'] = os.path.getsize(absfilename)
                response.write(botslib.readdata(currentta.filename,charset=currentta.charset,errors='ignore'))
                return response
            elif request.GET['action']=='previous':
                if currentta.parent:  #has a explicit parent
                    talijst = list(models.ta.objects.filter(idta=currentta.parent))
                else:   #get list of ta's referring to this idta as child
                    talijst = list(models.ta.objects.filter(child=currentta.idta))
            elif request.GET['action']=='this':
                if currentta.status == EXTERNIN:     #jump strait to next file, as EXTERNIN can not be displayed.
                    talijst = list(models.ta.objects.filter(parent=currentta.idta))
                elif currentta.status == EXTERNOUT:
                    talijst = list(models.ta.objects.filter(idta=currentta.parent))
                else:
                    talijst = [currentta]
            elif request.GET['action']=='next':
                if currentta.child: #has a explicit child
                    talijst = list(models.ta.objects.filter(idta=currentta.child))
                else:
                    talijst = list(models.ta.objects.filter(parent=currentta.idta))
            for ta in talijst:
                ta.has_next = True
                if ta.status == EXTERNIN:
                    ta.content = _(u'External file. Can not be displayed. Use "next".')
                elif ta.status == EXTERNOUT:
                    ta.content = _(u'External file. Can not be displayed. Use "previous".')
                    ta.has_next = False
                elif ta.statust in [OPEN,ERROR]:
                    ta.content = _(u'File has error status and does not exist. Use "previous".')
                    ta.has_next = False
                elif not ta.filename:
                    ta.content = _(u'File can not be displayed.')
                else:
                    if ta.charset:  #guess charset; uft-8 is reasonable
                        ta.content = botslib.readdata(ta.filename,charset=ta.charset,errors='ignore')
                    else:
                        ta.content = botslib.readdata(ta.filename,charset='utf-8',errors='ignore')
            return  django.shortcuts.render_to_response('bots/filer.html', {'idtas': talijst},context_instance=django.template.RequestContext(request))
        except:
            #~ print botslib.txtexc()
            return  django.shortcuts.render_to_response('bots/filer.html', {'error_content': nosuchfile},context_instance=django.template.RequestContext(request))
示例#2
0
def mailbag(ta_from,endstatus,**argv):
    ''' 2 main functions:
        -   recognizes and distuinguishes several edi types: x12 edifact tradacoms xml
            ('mailbag' in, correct editype out)
        -   split up interchanges (edifact, x12, tradacoms)
        details:
        - edifact, x12 and tradacoms can be can be mixed,
        - recognizes xml files; but messagetype 'xml' has a special handling when reading xml-files: xpath identifiers are used.
          this is needed for using xml in mailbag!
        - when more interchanges in one file: strongly recommended to mailbag/split these.
        - handle multiple UNA in one file, including different charsets.
        - handle multiple x12 seperators in one file.
    '''
    edifile = botslib.readdata(filename=ta_from.filename)       #read as binary...
    startpos = 0
    while (1):
        found = HEADER.search(edifile[startpos:])
        if found is None:
            if startpos:    #ISA/UNB have been found in file; no new ISA/UNB is found. So all processing is done.
                break
            #guess if this is an xml file.....
            sniffxml = edifile[:25]
            sniffxml = sniffxml.lstrip(' \t\n\r\f\v\xFF\xFE\xEF\xBB\xBF\x00')       #to find first ' real' data; some char are because of BOM, UTF-16 etc
            if sniffxml and sniffxml[0] == '<':
                ta_to = ta_from.copyta(status=endstatus,statust=OK,filename=ta_from.filename,editype='xml',messagetype='mailbag')  #make transaction for translated message; gets ta_info of ta_frommes
                #~ ta_tomes.update(status=STATUSTMP,statust=OK,filename=ta_set_for_processing.filename,editype='xml') #update outmessage transaction with ta_info;
                break
            else:
                raise botslib.InMessageError(_(u'Found no content in mailbag.'))
        elif found.group(1):
            editype = 'x12'
            headpos = startpos + found.start(2)
            count = 0
            for char in edifile[headpos:headpos+120]:  #search first 120 characters to find separators
                if char in '\r\n' and count != 105:
                    continue
                count += 1
                if count == 4:
                    field_sep = char
                elif count == 106:
                    record_sep = char
                    break
            #~ foundtrailer = re.search(re.escape(record_sep)+'\s*IEA'+re.escape(field_sep)+'.+?'+re.escape(record_sep),edifile[headpos:],re.DOTALL)
            foundtrailer = re.search(re.escape(record_sep)+'\s*I\s*E\s*A\s*'+re.escape(field_sep)+'.+?'+re.escape(record_sep),edifile[headpos:],re.DOTALL)
        elif found.group(3):
            editype = 'edifact'
            if found.group(4):
                field_sep = edifile[startpos + found.start(4) + 4]
                record_sep = edifile[startpos + found.start(4) + 8]
                headpos = startpos + found.start(4)
            else:
                field_sep = '+'
                record_sep = "'"
                headpos = startpos + found.start(5)
            foundtrailer = re.search(re.escape(record_sep)+'\s*U\s*N\s*Z\s*'+re.escape(field_sep)+'.+?'+re.escape(record_sep),edifile[headpos:],re.DOTALL)
        elif found.group(8):
            editype = 'tradacoms'
            headpos = startpos + found.start(9)
            field_sep = '='     #the tradacoms 'after-segment-tag-seperator'
            record_sep = "'"
            foundtrailer = re.search(re.escape(record_sep)+'\s*E\s*N\s*D\s*'+re.escape(field_sep)+'.+?'+re.escape(record_sep),edifile[headpos:],re.DOTALL)
        if not foundtrailer:
            raise botslib.InMessageError(_(u'Found no valid envelope trailer in mailbag.'))
        endpos = headpos + foundtrailer.end()
        #so: interchange is from headerpos untill endpos
        #~ if HEADER.search(edifile[headpos+25:endpos]):   #check if there is another header in the interchange
            #~ raise botslib.InMessageError(u'Error in mailbag format: found no valid envelope trailer.')
        ta_to = ta_from.copyta(status=endstatus)  #make transaction for translated message; gets ta_info of ta_frommes
        tofilename = str(ta_to.idta)
        tofile = botslib.opendata(tofilename,'wb')
        tofile.write(edifile[headpos:endpos])
        tofile.close()
        ta_to.update(statust=OK,filename=tofilename,editype=editype,messagetype=editype) #update outmessage transaction with ta_info;
        startpos = endpos
        botsglobal.logger.debug(_(u'        File written: "%s".'),tofilename)
示例#3
0
def mailbag(ta_from, endstatus, **argv):
    ''' 2 main functions:
        -   recognizes and distuinguishes several edi types: x12 edifact tradacoms xml
            ('mailbag' in, correct editype out)
        -   split up interchanges (edifact, x12, tradacoms)
        details:
        - edifact, x12 and tradacoms can be can be mixed,
        - recognizes xml files; but messagetype 'xml' has a special handling when reading xml-files: xpath identifiers are used.
          this is needed for using xml in mailbag!
        - when more interchanges in one file: strongly recommended to mailbag/split these.
        - handle multiple UNA in one file, including different charsets.
        - handle multiple x12 seperators in one file.
    '''
    edifile = botslib.readdata(filename=ta_from.filename)  #read as binary...
    startpos = 0
    while (1):
        found = HEADER.search(edifile[startpos:])
        if found is None:
            if startpos:  #ISA/UNB have been found in file; no new ISA/UNB is found. So all processing is done.
                break
            #guess if this is an xml file.....
            sniffxml = edifile[:25]
            sniffxml = sniffxml.lstrip(
                ' \t\n\r\f\v\xFF\xFE\xEF\xBB\xBF\x00'
            )  #to find first ' real' data; some char are because of BOM, UTF-16 etc
            if sniffxml and sniffxml[0] == '<':
                ta_to = ta_from.copyta(
                    status=endstatus,
                    statust=OK,
                    filename=ta_from.filename,
                    editype='xml',
                    messagetype='mailbag'
                )  #make transaction for translated message; gets ta_info of ta_frommes
                #~ ta_tomes.update(status=STATUSTMP,statust=OK,filename=ta_set_for_processing.filename,editype='xml') #update outmessage transaction with ta_info;
                break
            else:
                raise botslib.InMessageError(
                    _(u'Found no content in mailbag.'))
        elif found.group(1):
            editype = 'x12'
            headpos = startpos + found.start(2)
            count = 0
            for char in edifile[
                    headpos:headpos +
                    120]:  #search first 120 characters to find separators
                if char in '\r\n' and count != 105:
                    continue
                count += 1
                if count == 4:
                    field_sep = char
                elif count == 106:
                    record_sep = char
                    break
            #~ foundtrailer = re.search(re.escape(record_sep)+'\s*IEA'+re.escape(field_sep)+'.+?'+re.escape(record_sep),edifile[headpos:],re.DOTALL)
            foundtrailer = re.search(
                re.escape(record_sep) + '\s*I\s*E\s*A\s*' +
                re.escape(field_sep) + '.+?' + re.escape(record_sep),
                edifile[headpos:], re.DOTALL)
        elif found.group(3):
            editype = 'edifact'
            if found.group(4):
                field_sep = edifile[startpos + found.start(4) + 4]
                record_sep = edifile[startpos + found.start(4) + 8]
                headpos = startpos + found.start(4)
            else:
                field_sep = '+'
                record_sep = "'"
                headpos = startpos + found.start(5)
            foundtrailer = re.search(
                re.escape(record_sep) + '\s*U\s*N\s*Z\s*' +
                re.escape(field_sep) + '.+?' + re.escape(record_sep),
                edifile[headpos:], re.DOTALL)
        elif found.group(8):
            editype = 'tradacoms'
            headpos = startpos + found.start(9)
            field_sep = '='  #the tradacoms 'after-segment-tag-seperator'
            record_sep = "'"
            foundtrailer = re.search(
                re.escape(record_sep) + '\s*E\s*N\s*D\s*' +
                re.escape(field_sep) + '.+?' + re.escape(record_sep),
                edifile[headpos:], re.DOTALL)
        if not foundtrailer:
            raise botslib.InMessageError(
                _(u'Found no valid envelope trailer in mailbag.'))
        endpos = headpos + foundtrailer.end()
        #so: interchange is from headerpos untill endpos
        #~ if HEADER.search(edifile[headpos+25:endpos]):   #check if there is another header in the interchange
        #~ raise botslib.InMessageError(u'Error in mailbag format: found no valid envelope trailer.')
        ta_to = ta_from.copyta(
            status=endstatus
        )  #make transaction for translated message; gets ta_info of ta_frommes
        tofilename = str(ta_to.idta)
        tofile = botslib.opendata(tofilename, 'wb')
        tofile.write(edifile[headpos:endpos])
        tofile.close()
        ta_to.update(
            statust=OK,
            filename=tofilename,
            editype=editype,
            messagetype=editype)  #update outmessage transaction with ta_info;
        startpos = endpos
        botsglobal.logger.debug(_(u'        File written: "%s".'), tofilename)
示例#4
0
def filer(request, *kw, **kwargs):
    ''' handles bots file viewer. Only files in data dir of Bots are displayed.'''
    if request.method == 'GET':
        try:
            idta = request.GET['idta']
            if idta == 0:  #for the 'starred' file names (eg multiple output)
                raise Exception('to be caught')

            currentta = list(models.ta.objects.filter(idta=idta))[0]
            if request.GET['action'] == 'downl':
                response = django.http.HttpResponse(
                    content_type=currentta.contenttype)
                if currentta.contenttype == 'text/html':
                    dispositiontype = 'inline'
                else:
                    dispositiontype = 'attachment'
                response[
                    'Content-Disposition'] = dispositiontype + '; filename=' + currentta.filename + '.txt'
                #~ response['Content-Length'] = os.path.getsize(absfilename)
                response.write(botslib.readdata(currentta.filename))
                return response
            elif request.GET['action'] == 'previous':
                if currentta.parent:  #has a explicit parent
                    talijst = list(
                        models.ta.objects.filter(idta=currentta.parent))
                else:  #get list of ta's referring to this idta as child
                    talijst = list(
                        models.ta.objects.filter(idta__range=(currentta.script,
                                                              currentta.idta),
                                                 child=currentta.idta))
            elif request.GET['action'] == 'this':
                if currentta.status == EXTERNIN:  #EXTERNIN can not be displayed, so go to first FILEIN
                    talijst = list(
                        models.ta.objects.filter(parent=currentta.idta))
                elif currentta.status == EXTERNOUT:  #EXTERNOUT can not be displayed, so go to last FILEOUT
                    talijst = list(
                        models.ta.objects.filter(idta=currentta.parent))
                else:
                    talijst = [currentta]
            elif request.GET['action'] == 'next':
                if currentta.child:  #has a explicit child
                    talijst = list(
                        models.ta.objects.filter(idta=currentta.child))
                else:
                    talijst = list(
                        models.ta.objects.filter(parent=currentta.idta))
            for ta_object in talijst:
                #determine if can display file
                if ta_object.filename and ta_object.filename.isdigit():
                    if ta_object.charset:
                        ta_object.content = botslib.readdata(
                            ta_object.filename,
                            charset=ta_object.charset,
                            errors='ignore')
                    else:  #guess safe choice for charset. alt1: get charset by looking forward (until translation). alt2:try with utf-8, if error iso-8859-1
                        ta_object.content = botslib.readdata(
                            ta_object.filename,
                            charset='us-ascii',
                            errors='ignore')
                    ta_object.has_file = True
                    if ta_object.editype == 'x12':
                        ta_object.content = viewlib.indent_x12(
                            ta_object.content)
                    elif ta_object.editype == 'edifact':
                        ta_object.content = viewlib.indent_edifact(
                            ta_object.content)
                else:
                    ta_object.has_file = False
                    ta_object.content = _(u'No file available for display.')
                #determine has previous:
                if ta_object.parent or ta_object.status == MERGED:
                    ta_object.has_previous = True
                else:
                    ta_object.has_previous = False
                #determine: has next:
                if ta_object.status == EXTERNOUT or ta_object.statust in [
                        OPEN, ERROR
                ]:
                    ta_object.has_next = False
                else:
                    ta_object.has_next = True
            return django.shortcuts.render(request, 'bots/filer.html',
                                           {'idtas': talijst})
        except:
            return django.shortcuts.render(
                request, 'bots/filer.html',
                {'error_content': _(u'No such file.')})
示例#5
0
文件: views.py 项目: divadrei/bots
def filer(request,*kw,**kwargs):
    ''' handles bots file viewer. Only files in data dir of Bots are displayed.'''
    if request.method == 'GET':
        try:
            idta = request.GET['idta']
            if idta == 0: #for the 'starred' file names (eg multiple output)
                raise Exception('to be caught')
                
            currentta = list(models.ta.objects.filter(idta=idta))[0]
            if request.GET['action'] == 'downl':
                response = django.http.HttpResponse(mimetype=currentta.contenttype)
                if currentta.contenttype == 'text/html':
                    dispositiontype = 'inline'
                else:
                    dispositiontype = 'attachment'
                response['Content-Disposition'] = dispositiontype + '; filename=' + currentta.filename + '.txt'
                #~ response['Content-Length'] = os.path.getsize(absfilename)
                response.write(botslib.readdata(currentta.filename))
                return response
            elif request.GET['action'] == 'previous':
                if currentta.parent:    #has a explicit parent
                    talijst = list(models.ta.objects.filter(idta=currentta.parent))
                else:                   #get list of ta's referring to this idta as child
                    talijst = list(models.ta.objects.filter(child=currentta.idta))
            elif request.GET['action'] == 'this':
                if currentta.status == EXTERNIN:        #EXTERNIN can not be displayed, so go to first FILEIN
                    talijst = list(models.ta.objects.filter(parent=currentta.idta))
                elif currentta.status == EXTERNOUT:     #EXTERNOUT can not be displayed, so go to last FILEOUT
                    talijst = list(models.ta.objects.filter(idta=currentta.parent))
                else:
                    talijst = [currentta]
            elif request.GET['action'] == 'next':
                if currentta.child:     #has a explicit child
                    talijst = list(models.ta.objects.filter(idta=currentta.child))
                else: 
                    talijst = list(models.ta.objects.filter(parent=currentta.idta))
            for ta_object in talijst:
                #determine if can display file
                if ta_object.filename and ta_object.filename.isdigit():
                    if ta_object.charset:  
                        ta_object.content = botslib.readdata(ta_object.filename,charset=ta_object.charset,errors='ignore')
                    else:   #guess safe choice for charset. alt1: get charset by looking forward (until translation). alt2:try with utf-8, if error iso-8859-1   
                        ta_object.content = botslib.readdata(ta_object.filename,charset='us-ascii',errors='ignore')
                    ta_object.has_file = True
                    if ta_object.editype == 'x12':
                        ta_object.content = viewlib.indent_x12(ta_object.content)
                    elif ta_object.editype == 'edifact':
                        ta_object.content = viewlib.indent_edifact(ta_object.content)
                else:
                    ta_object.has_file = False
                    ta_object.content = _(u'No file available for display.')
                #determine has previous:
                if ta_object.parent or ta_object.status == MERGED:
                    ta_object.has_previous = True
                else:
                    ta_object.has_previous = False
                #determine: has next:
                if ta_object.status == EXTERNOUT or ta_object.statust in [OPEN,ERROR]:
                    ta_object.has_next = False
                else:
                    ta_object.has_next = True
            return  django.shortcuts.render_to_response('bots/filer.html', {'idtas': talijst},context_instance=django.template.RequestContext(request))
        except:
            return  django.shortcuts.render_to_response('bots/filer.html', {'error_content': _(u'No such file.')},context_instance=django.template.RequestContext(request))
示例#6
0
def mailbag(ta_from,endstatus,frommessagetype,**argv):
    ''' 2 main functions:
        -   recognizes and distuinguishes several edi types: x12 edifact tradacoms ('mailbag' in, correct editype out)
        -   split up interchanges (edifact, x12, tradacoms)
        details:
        - edifact, x12 and tradacoms can be can be mixed
        - handle multiple UNA in one file, including different charsets.
        - handle multiple ISA's with different separators in one file
        in bots > 3.0.0 all mailbag, edifact, x12 and tradacoms go via mailbag.
    '''
    edifile = botslib.readdata(filename=ta_from.filename)
    startpos = 0
    nr_interchanges = 0
    while (1):
        found = HEADER.match(edifile[startpos:])
        if found is None:
            if edifile[startpos:].strip(string.whitespace+'\x1A\x00'):  #there is content...but not valid
                if nr_interchanges:    #found interchanges, but remainder is not valid
                    raise botslib.InMessageError(_(u'[M50]: Found data not in a valid interchange at position %(pos)s.'),{'pos':startpos})                
                else:   #no interchanges found, content is not a valid edifact/x12/tradacoms interchange
                    if frommessagetype == 'mailbag':    #if indicated 'mailbag': guess if this is an xml file.....
                        sniffxml = edifile[:25]
                        sniffxml = sniffxml.lstrip(' \t\n\r\f\v\xFF\xFE\xEF\xBB\xBF\x00')       #to find first ' real' data; some char are because of BOM, UTF-16 etc
                        if sniffxml and sniffxml[0] == '<':
                            #is a xml file; inmessage.py can determine the right xml messagetype via xpath. 
                            filesize = len(edifile)
                            ta_to = ta_from.copyta(status=endstatus,statust=OK,filename=ta_from.filename,editype='xml',messagetype='mailbag',filesize=filesize)
                            return
                    raise botslib.InMessageError(_(u'[M51]: Edi file does not start with a valid interchange.'))
            else:   #no parseble content
                if nr_interchanges:    #OK: there are interchanges, but no new interchange is found.
                    return
                else:   #no edifact/x12/tradacoms envelope at all
                    raise botslib.InMessageError(_(u'[M52]: Edi file contains only whitespace.'))
        elif found.group('x12'):
            editype = 'x12'
            headpos = startpos + found.start('x12')
            #determine field_sep and record_sep
            count = 0
            for char in edifile[headpos:headpos+120]:  #search first 120 characters to determine separators
                if char in '\r\n' and count != 105:
                    continue
                count += 1
                if count == 4:
                    field_sep = char
                elif count in [7,18,21,32,35,51,54,70]:   #extra checks for fixed ISA. 
                    if char != field_sep:
                        raise botslib.InMessageError(_(u'[M53]: Non-valid ISA header at position %(pos)s; position %(pos_element)s of ISA is "%(foundchar)s", expect here element separator "%(field_sep)s".'),
                                                        {'pos':headpos,'pos_element':unicode(count),'foundchar':char,'field_sep':field_sep})
                elif count == 106:
                    record_sep = char
                    break
            foundtrailer = re.search('''%(record_sep)s
                                        \s*
                                        I[\n\r]*E[\n\r]*A
                                        .+?
                                        %(record_sep)s
                                        '''%{'record_sep':re.escape(record_sep)},
                                        edifile[headpos:],re.DOTALL|re.VERBOSE)
            if not foundtrailer:
                foundtrailer2 = re.search('''%(record_sep)s
                                            \s*
                                            I[\n\r]*E[\n\r]*A
                                            '''%{'record_sep':re.escape(record_sep)},
                                            edifile[headpos:],re.DOTALL|re.VERBOSE)
                if foundtrailer2:
                    raise botslib.InMessageError(_(u'[M60]: Found no segment terminator for IEA trailer at position %(pos)s.'),{'pos':foundtrailer2.start()})
                else:
                    raise botslib.InMessageError(_(u'[M54]: Found no valid IEA trailer for the ISA header at position %(pos)s.'),{'pos':headpos})
        elif found.group('edifact'):
            editype = 'edifact'
            headpos = startpos + found.start('edifact')
            #parse UNA. valid UNA: UNA:+.? '
            if found.group('UNA'):
                count = 0
                for char in found.group('UNAstring'):
                    if char in '\r\n':
                        continue
                    count += 1
                    if count == 2:
                        field_sep = char
                    elif count == 4:
                        escape = char
                    elif count == 6:
                        record_sep = char
                if count != 6 and len(found.group('UNAstring').rstrip()) != 6:
                    raise botslib.InMessageError(_(u'[M55]: Non-valid UNA-segment at position %(pos)s. UNA-segment should be 6 positions.'),{'pos':headpos})
                if found.group('field_sep') != field_sep:
                    raise botslib.InMessageError(_(u'[M56]: Data element separator used in edifact file differs from value indicated in UNA-segment.'))
            else:   #no UNA, interpret UNB
                if found.group('field_sep') == '+':
                    record_sep = "'"
                    escape = '?'
                elif found.group('field_sep') == '\x1D':        #according to std this was preffered way...probably quite theoretic...but does no harm
                    record_sep = '\x1C'
                    escape = ''
                else:
                    raise botslib.InMessageError(_(u'[M57]: Edifact file with non-standard separators. UNA segment should be used.'))
            #search trailer
            foundtrailer = re.search('''[^%(escape)s\n\r]       #char that is not escape or cr/lf
                                        [\n\r]*?                #maybe some cr/lf's
                                        %(record_sep)s          #segment separator
                                        \s*                     #whitespace between segments
                                        U[\n\r]*N[\n\r]*Z       #UNZ
                                        .+?                     #any chars
                                        [^%(escape)s\n\r]       #char that is not escape or cr/lf
                                        [\n\r]*?                #maybe some cr/lf's
                                        %(record_sep)s          #segment separator
                                        '''%{'escape':escape,'record_sep':re.escape(record_sep)},
                                        edifile[headpos:],re.DOTALL|re.VERBOSE)
            if not foundtrailer:
                raise botslib.InMessageError(_(u'[M58]: Found no valid UNZ trailer for the UNB header at position %(pos)s.'),{'pos':headpos})
        elif found.group('tradacoms'):
            editype = 'tradacoms'
            #~ field_sep = '='     #the tradacoms 'after-segment-tag-separator'
            record_sep = "'"
            escape = '?'
            headpos = startpos + found.start('STX')
            foundtrailer = re.search('''[^%(escape)s\n\r]       #char that is not escape or cr/lf
                                        [\n\r]*?                #maybe some cr/lf's
                                        %(record_sep)s          #segment separator
                                        \s*                     #whitespace between segments
                                        E[\n\r]*N[\n\r]*D
                                        .+?
                                        [^%(escape)s\n\r]       #char that is not escape or cr/lf
                                        [\n\r]*?                #maybe some cr/lf's
                                        %(record_sep)s          #segment separator
                                        '''%{'escape':escape,'record_sep':re.escape(record_sep)},
                                        edifile[headpos:],re.DOTALL|re.VERBOSE)
            if not foundtrailer:
                raise botslib.InMessageError(_(u'[M59]: Found no valid END trailer for the STX header at position %(pos)s.'),{'pos':headpos})
        #so: found an interchange (from headerpos until endpos)
        endpos = headpos + foundtrailer.end()
        ta_to = ta_from.copyta(status=endstatus)  #make transaction for translated message; gets ta_info of ta_frommes
        tofilename = unicode(ta_to.idta)
        filesize = len(edifile[headpos:endpos])
        tofile = botslib.opendata(tofilename,'wb')
        tofile.write(edifile[headpos:endpos])
        tofile.close()
        #editype is now either edifact, x12 or tradacoms
        #frommessagetype is the original frommessagetype (from route).
        #frommessagetype would normally be edifact, x12, tradacoms or mailbag, but could also be eg ORDERSD96AUNEAN007.
        #If so, we want to preserve that.
        if frommessagetype != 'mailbag' and frommessagetype != editype:
            messagetype = frommessagetype
        else:
            messagetype = editype
        ta_to.update(statust=OK,filename=tofilename,editype=editype,messagetype=messagetype,filesize=filesize) #update outmessage transaction with ta_info;
        startpos = endpos
        nr_interchanges += 1
        botsglobal.logger.debug(_(u'        File written: "%(tofilename)s".'),{'tofilename':tofilename})
示例#7
0
def filer(request, *kw, **kwargs):
    """ handles bots file viewer. Only files in data dir of Bots are displayed."""
    nosuchfile = _(u"No such file.")
    if request.method == "GET":
        try:
            idta = request.GET["idta"]
        except:
            return django.shortcuts.render_to_response(
                "bots/filer.html",
                {"error_content": nosuchfile},
                context_instance=django.template.RequestContext(request),
            )
        if idta == 0:  # for the 'starred' file names (eg multiple output)
            return django.shortcuts.render_to_response(
                "bots/filer.html",
                {"error_content": nosuchfile},
                context_instance=django.template.RequestContext(request),
            )
        try:
            currentta = list(models.ta.objects.filter(idta=idta))[0]
            if request.GET["action"] == "downl":
                response = django.http.HttpResponse(mimetype=currentta.contenttype)
                response["Content-Disposition"] = "attachment; filename=" + currentta.filename + ".txt"
                # ~ absfilename = botslib.abspathdata(currentta.filename)
                # ~ response['Content-Length'] = os.path.getsize(absfilename)
                response.write(botslib.readdata(currentta.filename, charset=currentta.charset, errors="ignore"))
                return response
            elif request.GET["action"] == "previous":
                if currentta.parent:  # has a explicit parent
                    talijst = list(models.ta.objects.filter(idta=currentta.parent))
                else:  # get list of ta's referring to this idta as child
                    talijst = list(models.ta.objects.filter(child=currentta.idta))
            elif request.GET["action"] == "this":
                if currentta.status == EXTERNIN:  # jump strait to next file, as EXTERNIN can not be displayed.
                    talijst = list(models.ta.objects.filter(parent=currentta.idta))
                elif currentta.status == EXTERNOUT:
                    talijst = list(models.ta.objects.filter(idta=currentta.parent))
                else:
                    talijst = [currentta]
            elif request.GET["action"] == "next":
                if currentta.child:  # has a explicit child
                    talijst = list(models.ta.objects.filter(idta=currentta.child))
                else:
                    talijst = list(models.ta.objects.filter(parent=currentta.idta))
            for ta in talijst:
                ta.has_next = True
                if ta.status == EXTERNIN:
                    ta.content = _(u'External file. Can not be displayed. Use "next".')
                elif ta.status == EXTERNOUT:
                    ta.content = _(u'External file. Can not be displayed. Use "previous".')
                    ta.has_next = False
                elif ta.statust in [OPEN, ERROR]:
                    ta.content = _(u'File has error status and does not exist. Use "previous".')
                    ta.has_next = False
                elif not ta.filename:
                    ta.content = _(u"File can not be displayed.")
                else:
                    if ta.charset:  # guess charset; uft-8 is reasonable
                        ta.content = botslib.readdata(ta.filename, charset=ta.charset, errors="ignore")
                    else:
                        ta.content = botslib.readdata(ta.filename, charset="utf-8", errors="ignore")
            return django.shortcuts.render_to_response(
                "bots/filer.html", {"idtas": talijst}, context_instance=django.template.RequestContext(request)
            )
        except:
            print botslib.txtexc()
            return django.shortcuts.render_to_response(
                "bots/filer.html",
                {"error_content": nosuchfile},
                context_instance=django.template.RequestContext(request),
            )
示例#8
0
def splitmailbag(startstatus=MAILBAG,endstatus=TRANSLATE,idroute=''):
    ''' splits 'mailbag'files to seperate files each containging one interchange (ISA-IEA or UNA/UNB-UNZ).
        handles x12 and edifact; these can be mixed.
    '''
    header = re.compile('(\s*(ISA))|(\s*(UNA.{6})?\s*(U\s*N\s*B)s*.{1}(.{4}).{1}(.{1}))',re.DOTALL)
    #           group:    1   2       3  4            5        6         7
    for row in botslib.query(u'''SELECT idta,filename,charset
                                FROM  ta
                                WHERE   idta>%(rootidta)s
                                AND     status=%(status)s
                                AND     statust=%(statust)s
                                AND     idroute=%(idroute)s
                                ''',
                                {'status':startstatus,'statust':OK,'idroute':idroute,'rootidta':botslib.get_minta4query()}):
        try:
            ta_org=botslib.OldTransaction(row['idta'])
            ta_intermediate = ta_org.copyta(status=MAILBAGPARSED)
            edifile = botslib.readdata(filename=row['filename'])    #read as binary...
            botsglobal.logmap.debug(u'Start parsing mailbag file "%s".',row['filename'])
            startpos=0
            while (1):
                found = header.search(edifile[startpos:])
                if found is None:
                    if startpos:    #ISA/UNB have been found in file; no new ISA/UNB is found. So all processing is done.
                        break
                    #guess if this is an xml file.....
                    sniffxml = edifile[:25]
                    sniffxml = sniffxml.lstrip(' \t\n\r\f\v\xFF\xFE\xEF\xBB\xBF\x00')       #to find first ' real' data; some char are because of BOM, UTF-16 etc
                    if sniffxml and sniffxml[0]=='<':
                        ta_tomes=ta_intermediate.copyta(status=endstatus)  #make transaction for translated message; gets ta_info of ta_frommes
                        ta_tomes.update(status=STATUSTMP,statust=OK,filename=row['filename'],editype='xml') #update outmessage transaction with ta_info;
                        break;
                    else:
                        raise botslib.InMessageError(_(u'Found no content in mailbag.'))
                elif found.group(1):
                    editype='x12'
                    headpos=startpos+ found.start(2)
                    count=0
                    for c in edifile[headpos:headpos+120]:  #search first 120 characters to find seperators
                        if c in '\r\n' and count!=105:
                            continue
                        count +=1
                        if count==4:
                            field_sep = c
                        elif count==106:
                            record_sep = c
                            break
                    foundtrailer = re.search(re.escape(record_sep)+'\s*IEA'+re.escape(field_sep)+'.+?'+re.escape(record_sep),edifile[headpos:],re.DOTALL)
                elif found.group(3):
                    editype='edifact'
                    if found.group(4):
                        field_sep = edifile[startpos + found.start(4) + 4]
                        record_sep = edifile[startpos + found.start(4) + 8]
                        headpos=startpos+ found.start(4)
                    else:
                        field_sep = '+'
                        record_sep = "'"
                        headpos=startpos+ found.start(5)
                    foundtrailer = re.search(re.escape(record_sep)+'\s*U\s*N\s*Z\s*'+re.escape(field_sep)+'.+?'+re.escape(record_sep),edifile[headpos:],re.DOTALL)
                if not foundtrailer:
                    raise botslib.InMessageError(_(u'Found no valid envelope trailer in mailbag.'))
                endpos = headpos+foundtrailer.end()
                #so: interchange is from headerpos untill endpos
                #~ if header.search(edifile[headpos+25:endpos]):   #check if there is another header in the interchange
                    #~ raise botslib.InMessageError(u'Error in mailbag format: found no valid envelope trailer.')
                ta_tomes=ta_intermediate.copyta(status=endstatus)  #make transaction for translated message; gets ta_info of ta_frommes
                tofilename = str(ta_tomes.idta)
                tofile = botslib.opendata(tofilename,'wb')
                tofile.write(edifile[headpos:endpos])
                tofile.close()
                ta_tomes.update(status=STATUSTMP,statust=OK,filename=tofilename,editype=editype,messagetype=editype) #update outmessage transaction with ta_info;
                startpos=endpos
        except:
            txt=botslib.txtexc()
            ta_intermediate.failure()
            ta_intermediate.update(statust=ERROR,errortext=txt)
        else:
            botsglobal.logmap.debug(u'OK Parsing mailbag file "%s".',row['filename'])
            ta_org.update(statust=DONE)
            ta_intermediate.succes(endstatus)
            ta_intermediate.update(statust=DONE)