Example #1
4
def test_ccode_with_unicode():
    domein = 'test'
    tests = [(u'key1',u'leftcode'),
            (u'key2',u'~!@#$%^&*()_+}{:";][=-/.,<>?`'),
            (u'key3',u'?érýúíó?ás??lzcn?'),
            (u'key4',u'?ë?ÿüïöä´¨???è?ùì'),
            (u'key5',u'òà???UIÕÃ?Ñ`~'),
            (u'key6',u"a\xac\u1234\u20ac\U00008000"),
            (u'key7',u"abc_\u03a0\u03a3\u03a9.txt"),
            (u'key8',u"?ÉRÝÚÍÓ?ÁS??LZCN??"),
            (u'key9',u"Ë?¨YܨIÏÏÖÄ???È?ÙÌÒ`À`Z?"),
            ]
    try:    #clean before test
        botslib.changeq(u'''DELETE FROM ccode ''')
        botslib.changeq(u'''DELETE FROM ccodetrigger''')
    except:
        print 'Error while deleting: ',botslib.txtexc()
        raise
    try:
        botslib.changeq(u'''INSERT INTO ccodetrigger (ccodeid)
                                VALUES (%(ccodeid)s)''',
                                {'ccodeid':domein})
        for key,value in tests:
            botslib.changeq(u'''INSERT INTO ccode (ccodeid_id,leftcode,rightcode,attr1,attr2,attr3,attr4,attr5,attr6,attr7,attr8)
                                    VALUES (%(ccodeid)s,%(leftcode)s,%(rightcode)s,'1','1','1','1','1','1','1','1')''',
                                    {'ccodeid':domein,'leftcode':key,'rightcode':value})
    except:
        print 'Error while updating: ',botslib.txtexc()
        raise
    try:
        for key,value in tests:
            print 'key',key
            for row in botslib.query(u'''SELECT rightcode
                                        FROM    ccode
                                        WHERE   ccodeid_id = %(ccodeid)s
                                        AND     leftcode = %(leftcode)s''',
                                        {'ccodeid':domein,'leftcode':key}):
                print '    ',key, type(row['rightcode']),type(value)
                if row['rightcode'] != value:
                    print 'failure in test "%s": result "%s" is not equal to "%s"'%(key,row['rightcode'],value)
                else:
                    print '    OK'
                break;
            else:
                print '??can not find testentry %s %s in db'%(key,value)
    except:
        print 'Error while quering db: ',botslib.txtexc()
        raise
Example #2
2
def testraise(expect, msg2, *args, **kwargs):
    try:
        raise botslib.BotsError(msg2, *args, **kwargs)
    except Exception, msg:
        if not isinstance(msg, unicode):
            msg = unicode(msg)
            #~ print 'not unicode',type(msg),expect
            #~ print 'Error xxx\n',msg
        if expect:
            if unicode(expect) != msg.strip():
                print expect, '(expected)'
                print msg, '(received)'
        txt = botslib.txtexc()
        if not isinstance(txt, unicode):
            print 'Error txt\n', txt
Example #3
0
def test_ccode_with_unicode():
    domein = 'test'
    tests = [
        (u'key1', u'leftcode'),
        (u'key2', u'~!@#$%^&*()_+}{:";][=-/.,<>?`'),
        (u'key3', u'?érýúíó?ás??lzcn?'),
        (u'key4', u'?ë?ÿüïöä´¨???è?ùì'),
        (u'key5', u'òà???UIÕÃ?Ñ`~'),
        (u'key6', u"a\xac\u1234\u20ac\U00008000"),
        (u'key7', u"abc_\u03a0\u03a3\u03a9.txt"),
        (u'key8', u"?ÉRÝÚÍÓ?ÁS??LZCN??"),
        (u'key9', u"Ë?¨YܨIÏÏÖÄ???È?ÙÌÒ`À`Z?"),
    ]
    try:  #clean before test
        botslib.changeq(u'''DELETE FROM ccode ''')
        botslib.changeq(u'''DELETE FROM ccodetrigger''')
    except:
        print('Error while deleting: ', botslib.txtexc())
        raise
    try:
        botslib.changeq(
            u'''INSERT INTO ccodetrigger (ccodeid)
                                VALUES (%(ccodeid)s)''', {'ccodeid': domein})
        for key, value in tests:
            botslib.changeq(
                u'''INSERT INTO ccode (ccodeid_id,leftcode,rightcode,attr1,attr2,attr3,attr4,attr5,attr6,attr7,attr8)
                                    VALUES (%(ccodeid)s,%(leftcode)s,%(rightcode)s,'1','1','1','1','1','1','1','1')''',
                {
                    'ccodeid': domein,
                    'leftcode': key,
                    'rightcode': value
                })
    except:
        print('Error while updating: ', botslib.txtexc())
        raise
    try:
        for key, value in tests:
            print('key', key)
            for row in botslib.query(
                    u'''SELECT rightcode
                                        FROM    ccode
                                        WHERE   ccodeid_id = %(ccodeid)s
                                        AND     leftcode = %(leftcode)s''', {
                        'ccodeid': domein,
                        'leftcode': key
                    }):
                print('    ', key, type(row[str('rightcode')]), type(value))
                if row[str('rightcode')] != value:
                    print(
                        'failure in test "%s": result "%s" is not equal to "%s"'
                        % (key, row['rightcode'], value))
                else:
                    print('    OK')
                break
            else:
                print('??can not find testentry %s %s in db' % (key, value))
    except:
        print('Error while quering db: ', botslib.txtexc())
        raise
Example #4
0
def ubl_preprocess(ta_from, endstatus, *args, **kwargs):
    """Parse incoming UBL to find his type"""
    try:
        # copy ta for preprocessing
        ta_to = ta_from.copyta(status=endstatus)
        # Get UBL type (/CreditNote, /Invoice)
        doc = etree.parse(botslib.abspathdata(ta_from.filename))
        root = doc.getroot()
        if not root.tag.startswith(
                '{urn:oasis:names:specification:ubl:schema:xsd'):
            raise Exception('Incoming file is not an UBL file.')
        ubltype = root.tag.split('}')[-1]
        botsglobal.logger.debug('UBL type: %s', ubltype)
        if ubltype == 'CreditNote':
            ta_to.update(messagetype='UBL21_%s' % ubltype)
        elif ubltype != 'Invoice':
            raise Exception(
                'Incoming UBL file is not an Invoice or CreditNote.')
        # write out file
        filename = str(ta_to.idta)
        botslib.opendata_bin(filename, 'wb').write(
            botslib.opendata_bin(ta_from.filename, 'rb').read())
        # update ta
        ta_to.update(statust=OK, filename=filename)

    except:
        txt = botslib.txtexc()
        botsglobal.logger.error('UBL preprocess failed: %s', txt)
        raise botslib.InMessageError('UBL preprocess failed: %(error)s',
                                     error=txt)
Example #5
0
def startmulti(grammardir):
    ''' specialized tool for bulk checking of grammars while developing botsgrammars
        grammardir: directory with gramars (eg bots/usersys/grammars/edifact)
        editype: eg edifact
    '''
    configdir = 'config'
    botsinit.generalinit(
        configdir)  # find locating of bots, configfiles, init paths etc.
    process_name = 'grammarcheck'
    botsglobal.logger = botsinit.initenginelogging(process_name)
    atexit.register(logging.shutdown)
    files = glob.iglob(grammardir, recursive=True)
    files = [f for f in files if os.path.isfile(f)]
    for filename in files:
        filename_basename = os.path.basename(filename)
        editype = os.path.basename(os.path.dirname(filename))
        if filename_basename in ['__init__.py', '__pycache__', 'envelope.py']:
            continue
        if filename_basename.startswith(
                'edifact') or filename_basename.startswith(
                    'records') or filename_basename.endswith('records.py'):
            continue
        if not filename_basename.endswith('py'):
            continue
        filename_noextension = os.path.splitext(filename_basename)[0]
        try:
            grammar.grammarread(editype,
                                filename_noextension,
                                typeofgrammarfile='grammars')
        except:
            print(botslib.txtexc(), end='\n\n')
            sys.exit(1)
        else:
            print('OK - no error found in grammar', filename, end='\n\n')
Example #6
0
def testraise(expect, msg2, *args, **kwargs):
    try:
        raise botslib.BotsError(msg2, *args, **kwargs)
    except Exception as msg:
        if not isinstance(msg, unicode):
            msg = unicode(msg)
            #~ print 'not unicode',type(msg),expect
            #~ print 'Error xxx\n',msg
        if expect:
            if unicode(expect) != msg.strip():
                print(expect, '(expected)')
                print(msg, '(received)')
        txt = botslib.txtexc()
        if not isinstance(txt, unicode):
            print('Error txt\n', txt)
def cii_preprocess(ta_from, endstatus, *args, **kwargs):
    """Parse incoming CII to find his type"""
    try:
        # copy ta for preprocessing
        ta_to = ta_from.copyta(status=endstatus)

        # Get typecode by parsing incoming CII to /CrossIndustryInvoice /ExchangedDocument /TypeCode
        doc = etree.parse(botslib.abspathdata(ta_from.filename))
        namespaces = {
            'rsm':
            'urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100',
            'ram':
            'urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100',
        }
        root = doc.getroot()
        if root.tag != '{%s}CrossIndustryInvoice' % namespaces['rsm']:
            raise Exception(
                'Incoming file is not a CrossIndustryInvoice file.')
        typecode = doc.find('.//rsm:ExchangedDocument/ram:TypeCode',
                            namespaces=namespaces)
        typecode = typecode.text
        botsglobal.logger.debug('CII typecode: %s', typecode)
        # 380=Invoice, 381=Avoir > UBL CreditNote
        if typecode == '381':
            ta_to.update(alt='cii2creditnote')
        # write out file
        filename = str(ta_to.idta)
        botslib.opendata_bin(filename, 'wb').write(
            botslib.opendata_bin(ta_from.filename, 'rb').read())
        # update ta
        ta_to.update(statust=OK, filename=filename)

    except:
        txt = botslib.txtexc()
        botsglobal.logger.error('CII preprocess failed: %s', txt)
        raise botslib.InMessageError('CII preprocess failed: %(error)s',
                                     error=txt)
Example #8
0
def start():

    try:
        botsinit.generalinit('config')
        botsinit.initenginelogging()
    except:
        print 'Error reading bots.ini (before database connection).'
        raise

    try:
        botsinit.connect()
    except:
        print 'Could not connect to database.'
        raise

    #*****************start test***************
    domein = 'test'
    tests = [
        (u'key1', u'leftcode'),
        (u'key2', u'~!@#$%^&*()_+}{:";][=-/.,<>?`'),
        (u'key3', u'?érýúíó?ás??lzcn?'),
        (u'key4', u'?ë?ÿüïöä´¨???è?ùì'),
        (u'key5', u'òà???UIÕÃ?Ñ`~'),
        (u'key6', u"a\xac\u1234\u20ac\U00008000"),
        (u'key7', u"abc_\u03a0\u03a3\u03a9.txt"),
        (u'key8', u"?ÉRÝÚÍÓ?ÁS??LZCN??"),
        (u'key9', u"Ë?¨YܨIÏÏÖÄ???È?ÙÌÒ`À`Z?"),
    ]

    try:  #clean before test
        botslib.change(u'''DELETE FROM ccode ''')
        botslib.change(u'''DELETE FROM ccodetrigger''')
    except:
        print 'Error while deleting', botslib.txtexc()
        raise

    try:
        botslib.change(
            u'''INSERT INTO ccodetrigger (ccodeid)
                                VALUES (%(ccodeid)s)''', {'ccodeid': domein})
        for key, value in tests:
            botslib.change(
                u'''INSERT INTO ccode (ccodeid_id,leftcode,rightcode,attr1,attr2,attr3,attr4,attr5,attr6,attr7,attr8)
                                    VALUES (%(ccodeid)s,%(leftcode)s,%(rightcode)s,'1','1','1','1','1','1','1','1')''',
                {
                    'ccodeid': domein,
                    'leftcode': key,
                    'rightcode': value
                })
    except:
        print 'Error while updating', botslib.txtexc()
        raise

    try:
        for key, value in tests:
            print 'key', key
            for row in botslib.query(
                    u'''SELECT rightcode
                                        FROM    ccode
                                        WHERE   ccodeid_id = %(ccodeid)s
                                        AND     leftcode = %(leftcode)s''', {
                        'ccodeid': domein,
                        'leftcode': key
                    }):
                print '    ', key, type(row['rightcode']), type(value)
                if row['rightcode'] != value:
                    print 'failure in test "%s": result "%s" is not equal to "%s"' % (
                        key, row['rightcode'], value)
                else:
                    print '    OK'
                break
            else:
                print '??can not find testentry %s %s in db' % (key, value)
    except:
        print 'Error while quering db', botslib.txtexc()
        raise

    #*****************end test***************
    botsglobal.db.close()
Example #9
0
def startall():
    #NOTE: bots directory should always be on PYTHONPATH - otherwise it will not start.
    #********command line arguments**************************
    usage = '''
    This is "%(name)s" version %(version)s, part of Bots open source edi translator (http://bots.sourceforge.net).
    Checks all installed bots grammars. Same checks are used as in translations with bots-engine.
    
    Usage:  %(name)s  -c<directory>
    Options:
        -c<directory>   directory for configuration files (default: config).
    Examples:
        %(name)s -cconfig

    ''' % {
        'name': os.path.basename(sys.argv[0]),
        'version': botsglobal.version
    }
    configdir = 'config'
    for arg in sys.argv[1:]:
        if arg.startswith('-c'):
            configdir = arg[2:]
            if not configdir:
                print(
                    'Error: configuration directory indicated, but no directory name.'
                )
                sys.exit(1)
        else:
            print(usage)
            sys.exit(0)
    #***end handling command line arguments**************************

    botsinit.generalinit(
        configdir)  # find locating of bots, configfiles, init paths etc.
    process_name = 'grammarcheck'
    botsglobal.logger = botsinit.initenginelogging(process_name)
    atexit.register(logging.shutdown)

    usersysdirectory = botsglobal.ini['directories']['usersysabs']
    files = glob.iglob(usersysdirectory + '/grammars/**', recursive=True)
    files = [f for f in files if os.path.isfile(f)]
    for filename in files:
        filename_basename = os.path.basename(filename)
        editype = os.path.basename(os.path.dirname(filename))
        if filename_basename in ['__init__.py', '__pycache__', 'envelope.py']:
            continue
        if filename_basename.startswith(
                'edifact') or filename_basename.startswith(
                    'records') or filename_basename.endswith('records.py'):
            continue
        if not filename_basename.endswith('py'):
            continue
        filename_noextension = os.path.splitext(filename_basename)[0]
        try:
            grammar.grammarread(editype,
                                filename_noextension,
                                typeofgrammarfile='grammars')
        except:
            print(botslib.txtexc(), end='\n\n')
            sys.exit(1)
        else:
            print('OK - no error found in grammar', filename, end='\n\n')
Example #10
0
def start():
    #NOTE: bots directory should always be on PYTHONPATH - otherwise it will not start.
    #********command line arguments**************************
    usage = '''
    This is "%(name)s" version %(version)s, part of Bots open source edi translator (http://bots.sourceforge.net).
    Checks a Bots grammar. Same checks are used as in translations with bots-engine. Searches for grammar in
    regular place: bots/usersys/grammars/<editype>/<messagetype>.py  (even if a path is passed).

    Usage:  %(name)s  -c<directory> <editype> <messagetype>
       or   %(name)s  -c<directory> <path to grammar>
    Options:
        -c<directory>   directory for configuration files (default: config).
    Examples:
        %(name)s -cconfig  edifact  ORDERSD96AUNEAN008
        %(name)s -cconfig  C:/python27/lib/site-packages/bots/usersys/grammars/edifact/ORDERSD96AUNEAN008.py

    ''' % {
        'name': os.path.basename(sys.argv[0]),
        'version': botsglobal.version
    }
    configdir = 'config'
    editype = ''
    messagetype = ''
    for arg in sys.argv[1:]:
        if arg.startswith('-c'):
            configdir = arg[2:]
            if not configdir:
                print(
                    'Error: configuration directory indicated, but no directory name.'
                )
                sys.exit(1)
        elif arg in ['?', '/?', '-h', '--help'] or arg.startswith('-'):
            print(usage)
            sys.exit(0)
        else:
            if os.path.isfile(arg):
                p1, p2 = os.path.split(arg)
                editype = os.path.basename(p1)
                messagetype, ext = os.path.splitext(p2)
                messagetype = unicode(messagetype)
                print('grammarcheck', editype, messagetype)
            elif not editype:
                editype = arg
            else:
                messagetype = arg
    if not (editype and messagetype):
        print(
            'Error: both editype and messagetype, or a file path, are required.'
        )
        sys.exit(1)
    #***end handling command line arguments**************************
    botsinit.generalinit(
        configdir)  # find locating of bots, configfiles, init paths etc.
    process_name = 'grammarcheck'
    botsglobal.logger = botsinit.initenginelogging(process_name)
    atexit.register(logging.shutdown)

    try:
        grammar.grammarread(editype, messagetype, typeofgrammarfile='grammars')
    except:
        print('Found error in grammar: ', botslib.txtexc())
        sys.exit(1)
    else:
        print('OK - no error found in grammar')
        sys.exit(0)