Exemplo n.º 1
0
 def read_from_file(self, path):
     configdir = 'config'
     botsinit.generalinit(configdir)
     process_name = 'odoo_get_edi'
     botsglobal.logger = botsinit.initenginelogging(process_name)
     atexit.register(logging.shutdown)
     ta_info = {
         'alt': '',
         'charset': '',
         'command': 'new',
         'editype': 'edifact',
         'filename': path,
         'fromchannel': '',
         'frompartner': '',
         'idroute': '',
         'messagetype': 'edifact',
         'testindicator': '',
         'topartner': ''}
     try:
         edifile = inmessage.parse_edi_file(**ta_info)
     except Exception as e:
         if '[A59]' in str(e):
             raise exceptions.Warning(
                 _('Edi file has codification errors.'),
                 _('Check accents and other characters not allowed '
                   'in the edi document'))
         raise exceptions.Warning(_(
             'It has occurred following error: %s.' % e))
     json_ins = outmessage.json(edifile.ta_info)
     struc = [{ms.root.record['BOTSID']:
              json_class._node2json(json_ins, ms.root)}
              for ms in edifile.nextmessage()]
     return struc
Exemplo n.º 2
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')
Exemplo n.º 3
0
def start():
    #NOTE: bots directory should always be on PYTHONPATH - otherwise it will not start.
    #***command line arguments**************************
    #if config (-c option) is before job argument, will be used as config-dir of job2queue it self and not for job
    #if config (-c option) is after job argument, use both as config-dir of job2queue and as -c option of job.
    #if config (-c option) is before and after job argument, use only the after...could change that but seems not to be useful.
    usage = '''
    This is "%(name)s" version %(version)s, part of Bots open source edi translator (http://bots.sourceforge.net).
    Places a job in the bots jobqueue. Bots jobqueue takes care of correct processing of jobs.    
    Usage:
        %(name)s  [-c<directory>] [-p<priority>] job [job-parameters]
    Options:
        -c<directory>   directory for configuration files (default: config).
        -p<priority>    priority of job, 1-9 (default: 5, highest priority is 1).
    Example of usage:
        %(name)s bots-engine.py
        %(name)s python2.7 /usr/local/bin/bots-engine.py
        %(name)s -p1 python2.7 /usr/local/bin/bots-engine.py -cconfig2 myroute
        
    ''' % {
        'name': os.path.basename(sys.argv[0]),
        'version': botsglobal.version
    }
    configdir = 'config'  #default value
    priority = 5  #default value
    task_args = []
    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)
            if task_args:
                task_args.append(arg)
        elif arg.startswith('-p'):
            try:
                priority = int(arg[2:])
            except:
                print(
                    'Error: priority should be numeric (1=highest, 9=lowest).')
                sys.exit(1)
        elif arg in ["?", "/?", '-h', '--help']:
            print(usage)
            sys.exit(0)
        else:
            task_args.append(arg)
    #***end handling command line arguments**************************
    botsinit.generalinit(configdir)  #needed to read config
    if not botsglobal.ini.getboolean('jobqueue', 'enabled', False):
        print('Error: bots jobqueue cannot start; not enabled in %s/bots.ini' %
              configdir)
        sys.exit(1)

    terug = send_job_to_jobqueue(task_args, priority)
    print(JOBQUEUEMESSAGE2TXT[terug])
    sys.exit(terug)
Exemplo n.º 4
0
 def compose_edi_invoice(self, invoice):
     configdir = 'config'
     botsinit.generalinit(configdir)
     process_name = 'odoo_get_edi'
     botsglobal.logger = botsinit.initenginelogging(process_name)
     una, header, footer = self.get_edi_invoice_envelops(invoice)
     body = self.get_edi_invoice_body(invoice)
     content = ''.join([una, header, body, footer])
     return content
Exemplo n.º 5
0
def start(configdir):
    """Server program that ensures only a single bots-engine runs at any time,
    and no engine run requests are lost/discarded.

    Each request goes to a queue and is run in sequence when the previous run completes.
    Use of the job queue is optional and must be configured in bots.ini (jobqueue section, enabled = True).
    """

    botsinit.generalinit(configdir)
    if not botsglobal.ini.getboolean('jobqueue', 'enabled', False):
        print('Error: bots jobqueue cannot start; not enabled in {}/bots.ini'.
              format(configdir))
        sys.exit(1)
    nr_threads = 2  # botsglobal.ini.getint('jobqueue','nr_threads')
    process_name = 'jobqueue'

    logger = botsinit.initserverlogging(process_name)
    logger.log(25, 'Bots %(process_name)s started.',
               {'process_name': process_name})
    logger.log(
        25, 'Bots %(process_name)s configdir: "%(configdir)s".', {
            'process_name': process_name,
            'configdir': botsglobal.ini.get('directories', 'config')
        })
    port = botsglobal.ini.getint('jobqueue', 'port', 28082)
    logger.log(
        25, 'Bots %(process_name)s listens for xmlrpc at port: "%(port)s".', {
            'process_name': process_name,
            'port': port
        })

    # start launcher thread
    q = queue.Queue()

    lauchfrequency = botsglobal.ini.getint('jobqueue', 'lauchfrequency', 5)
    maxruntime = botsglobal.ini.getint('settings', 'maxruntime', 60)
    for thread in range(nr_threads):
        launcher_thread = threading.Thread(
            name='launcher',
            target=launcher,
            args=(logger, q, lauchfrequency, maxruntime),
        )
        launcher_thread.start()

    # the main thread is the xmlrpc server:
    # all adding, getting etc for jobqueue is done via xmlrpc.
    logger.info('Jobqueue server started.')
    server = SimpleXMLRPCServer(('localhost', port), logRequests=False)
    server.register_instance(Jobqueue(logger))

    try:
        server.serve_forever()
    except (KeyboardInterrupt, SystemExit) as e:  # noqa
        pass

    sys.exit(0)
Exemplo n.º 6
0
def start(configdir):
    """Server program that ensures only a single bots-engine runs at any time,
    and no engine run requests are lost/discarded.

    Each request goes to a queue and is run in sequence when the previous run completes.
    Use of the job queue is optional and must be configured in bots.ini (jobqueue section, enabled = True).
    """

    botsinit.generalinit(configdir)
    if not botsglobal.ini.getboolean('jobqueue', 'enabled', False):
        print('Error: bots jobqueue cannot start; not enabled in {}/bots.ini'.format(configdir))
        sys.exit(1)
    nr_threads = 2  # botsglobal.ini.getint('jobqueue','nr_threads')
    process_name = 'jobqueue'

    logger = botsinit.initserverlogging(process_name)
    logger.log(25, 'Bots %(process_name)s started.', {'process_name': process_name})
    logger.log(25, 'Bots %(process_name)s configdir: "%(configdir)s".', {
               'process_name': process_name, 'configdir': botsglobal.ini.get('directories', 'config')})
    port = botsglobal.ini.getint('jobqueue', 'port', 28082)
    logger.log(25, 'Bots %(process_name)s listens for xmlrpc at port: "%(port)s".',
               {'process_name': process_name, 'port': port})

    # start launcher thread
    q = queue.Queue()

    lauchfrequency = botsglobal.ini.getint('jobqueue', 'lauchfrequency', 5)
    maxruntime = botsglobal.ini.getint('settings', 'maxruntime', 60)
    for thread in range(nr_threads):
        launcher_thread = threading.Thread(
            name='launcher',
            target=launcher,
            args=(logger, q, lauchfrequency, maxruntime),
            )
        launcher_thread.start()

    # the main thread is the xmlrpc server:
    # all adding, getting etc for jobqueue is done via xmlrpc.
    logger.info('Jobqueue server started.')
    server = SimpleXMLRPCServer(('localhost', port), logRequests=False)
    server.register_instance(Jobqueue(logger))

    try:
        server.serve_forever()
    except (KeyboardInterrupt, SystemExit) as e:  # noqa
        pass

    sys.exit(0)
Exemplo n.º 7
0
                                ORDER BY idta DESC
                                ''',
                                {'status':310,'statust':DONE,'idroute':'testcontrl','confirmtype':'send-edifact-CONTRL','confirmasked':True}):
            counter += 1
            if counter <= 2:
                self.failUnless(row[1])
                self.failUnless(row[2]!=0)
            else:
                break
        else:
            self.failUnless(counter!=0)


if __name__ == '__main__':
    pythoninterpreter = 'python'
    newcommand = [pythoninterpreter,'bots-engine.py',]
    shutil.rmtree(os.path.join(botssys, 'outfile'),ignore_errors=True)    #remove whole output directory
    subprocess.call(newcommand)
    botsinit.generalinit('config')
    botsinit.initenginelogging()
    botsinit.connect() 
    print '''expect:
    21 files received/processed in run.
    17 files without errors,
    4 files with errors,
    30 files send in run.
    '''
    unittest.main()
    logging.shutdown()
    botsglobal.db.close()
Exemplo n.º 8
0
        raise Exception('expect exception in test_partner_lookup')


def grammartest(l, expect_error=True):
    if expect_error:
        if not subprocess.call(l):
            raise Exception('grammartest: expected error, but no error')
    else:
        if subprocess.call(l):
            raise Exception(
                'grammartest: expected no error, but received an error')


if __name__ == '__main__':
    pythoninterpreter = 'python2.7'
    botsinit.generalinit('config')
    utilsunit.dummylogger()
    utilsunit.cleanoutputdir()
    botssys = botsglobal.ini.get('directories', 'botssys')
    botsinit.connect()
    #**************************************************************************************************************************************
    #test references **********************************************************************************************************************
    utilsunit.RunTestCompareResults(
        [pythoninterpreter, 'bots-engine.py', 'testreference'], {
            'status': 0,
            'lastreceived': 1,
            'lasterror': 0,
            'lastdone': 1,
            'lastok': 0,
            'lastopen': 0,
            'send': 1,
Exemplo n.º 9
0
                "desadv1.edi",
                "",
            )
            print(
                "expect: <idta>.edi                          ",
                comclass.filename_formatter("{idta}.edi", ta),
            )
            self.assertRaises(botslib.CommunicationOutError,
                              comclass.filename_formatter, "{tada}", ta)
            self.assertRaises(
                botslib.CommunicationOutError,
                comclass.filename_formatter,
                "{infile:test}",
                ta,
            )
            if count == 1:  # test only 1 incoming files
                break


if __name__ == "__main__":
    pythoninterpreter = "python3.4"
    newcommand = [pythoninterpreter, "bots-engine.py"]
    subprocess.call(newcommand)

    botsinit.generalinit("config")
    botsinit.initenginelogging("engine")
    botsinit.connect()
    unittest.main()
    logging.shutdown()
    botsglobal.db.close()
Exemplo n.º 10
0
def start(route, prog, *args):
    """Start translation/validation of ubl/cii invoice"""

    start_logging()
    usage = """
%(prog)s v%(version)s

Usage: %(prog)s [options] input_%(intype)s.xml output_%(outtype)s.xml

    -R|--replace Replace output file if exist

    -r|--report  Build invoice validation report <filename>.report.xml

    -v|--validate
                 Do validation of input %(intype)s and output %(outtype)s translated.
                 (A valid installation of java is requiered.)

    --version    return version information and exit

    input:       Input %(intype)s file path to transtate
    output:      Output translated %(outtype)s file path

Author: %(author)s\
"""

    intype, outtype = prog.split('2')
    usage %= {
        'prog': prog,
        'intype': intype.upper(),
        'outtype': outtype.upper(),
        'version': __about__.__version__,
        'author': __about__.__author__,
    }

    infile = outfile = None
    replaceexisting = False
    do_validation = False
    build_report = False
    config_dir = 'config_ublcii'
    if not args:
        args = sys.argv[1:]
    for arg in args:
        if arg in ['-R', '--replace']:
            replaceexisting = True
        elif arg == '-D':
            globals()['DEBUG'] = True
            logger.setLevel(logging.DEBUG)
        elif arg == '--version':
            sys.stdout.write('%s %s%s' %
                             (prog, __about__.__version__, os.linesep))
            return
        elif arg.startswith('-c'):
            config_dir = arg[2:]
        elif arg == '-V':
            globals()['VERBOSE'] = True
        elif arg in ['-r', '--report']:
            build_report = True
        elif arg in ['-v', '--validate']:
            do_validation = True
        elif arg in ['?', '/?', '-h', '--help'] or arg.startswith('-'):
            info(usage)
            return 0
        elif not infile:
            infile = arg
        elif not outfile:
            outfile = arg
        else:
            error(usage)
            return 4

    if not infile:
        error('No input file specified', usage)
        return 4

    infile = os.path.abspath(infile)
    if not os.path.isfile(infile):
        error("Invalid input file: '%s'" % infile)
        return 5
    info("Input %s file: '%s'" % (intype.upper(), os.path.basename(infile)))
    logger.debug("Input %s file: '%s'", intype.upper(), infile)

    # Input validation
    if do_validation or build_report:
        info("Validating input %s invoice '%s' ..." % (intype.upper(), infile))
        report = None if build_report else tempfile.mktemp()
        try:
            report = build_validation_report(intype, infile, report)
        except:
            error(traceback.format_exc(limit=0))
            return 6
        ret = int(not bool(report))
        if report and do_validation:
            is_valid = parse_validation_report(report)
            ret = int(not bool(is_valid))
        if not build_report:
            os.remove(report)
        if not outfile:
            return ret

    if not outfile:
        error('No output file path specified', usage)
        return 4
    outfile = os.path.abspath(outfile)
    if os.path.isfile(outfile) and not replaceexisting:
        error(
            "Output file exist: '%s'\nUse -R or --replace to replace existing file"
            % outfile)
        return 5
    elif not os.path.isdir(os.path.dirname(outfile)):
        error('Output path is not a valid directory: %s' % outfile)
        return 5

    os.environ["BOTS_FILE_IN"] = infile
    os.environ["BOTS_FILE_OUT"] = outfile

    config_arg = '-c%s' % config_dir

    pargs = ['bots-engine', config_arg, route]

    try:
        proc = subprocess.Popen(pargs, stdout=STDOUT, stderr=STDERR)
        stdoutdata, stderrdata = proc.communicate()
        if VERBOSE:
            if not isinstance(stdoutdata, str):
                stdoutdata = stdoutdata.decode()
            if not isinstance(stderrdata, str):
                stderrdata = stderrdata.decode()
            info(stdoutdata)
            info(stderrdata)
        returncode = proc.returncode
        if returncode == 0:
            info("Output %s file translated: '%s'" %
                 (outtype.upper(), os.path.basename(outfile)))
            logger.debug("Output %s file translated: '%s'", outtype.upper(),
                         outfile)
            # Output validation
            if do_validation or build_report:
                info("Validating output %s invoice '%s' ..." %
                     (outtype, outfile))
                report = None if build_report else tempfile.mktemp()
                try:
                    report = build_validation_report(outtype, outfile, report)
                except:
                    error(traceback.format_exc(limit=0))
                    return 7
                if report and do_validation:
                    is_valid = parse_validation_report(report)
                    returncode = int(not bool(is_valid))
                if not build_report:
                    os.remove(report)
        elif returncode == 3:
            error(
                'Another instance of translator is running, try again in a few moment'
            )
        elif returncode == 2:
            error('ERROR during translating %s file: %s ' %
                  (intype.upper(), os.path.basename(outfile)))
            botsinit.generalinit(config_dir)
            from bots.models import filereport

            filerep = filereport.objects.filter(infilename=infile).exclude(
                statust=3).order_by('-idta').first()
            for err in filerep.errortext.split('MessageError:')[1:]:
                err = err.strip().replace(' "BOTSCONTENT"',
                                          '').replace('Exception: ', '')
                error(err)
        elif returncode != 0:
            error('Unexpected error %s occured' % returncode,
                  'proc: %s' % repr(proc.__dict__))
            return returncode
        del os.environ["BOTS_FILE_IN"]
        del os.environ["BOTS_FILE_OUT"]
        return returncode

    except Exception as exc:
        error(traceback.format_exc())
        del os.environ["BOTS_FILE_IN"]
        del os.environ["BOTS_FILE_OUT"]
        error('Exception occured: %s' % exc)
Exemplo n.º 11
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()
Exemplo n.º 12
0
            self.assertEqual(
                comclass.filename_formatter("*_{datetime:%Y-%m-%d}.edi", ta),
                str(sub_unique + 5) + "_" + datetime.datetime.now().strftime("%Y-%m-%d") + ".edi",
                "",
            )
            self.assertEqual(
                comclass.filename_formatter("*_*.edi", ta), str(sub_unique + 6) + "_" + str(sub_unique + 6) + ".edi", ""
            )
            self.assertEqual(comclass.filename_formatter("123.edi", ta), "123.edi", "")
            self.assertEqual(comclass.filename_formatter("{infile}", ta), "desadv1.edi", "")
            self.assertEqual(comclass.filename_formatter("{infile:name}.txt", ta), "desadv1.txt", "")
            self.assertEqual(comclass.filename_formatter("{infile:name}.{infile:ext}", ta), "desadv1.edi", "")
            print("expect: <idta>.edi                          ", comclass.filename_formatter("{idta}.edi", ta))
            self.assertRaises(botslib.CommunicationOutError, comclass.filename_formatter, "{tada}", ta)
            self.assertRaises(botslib.CommunicationOutError, comclass.filename_formatter, "{infile:test}", ta)
            if count == 1:  # test only 1 incoming files
                break


if __name__ == "__main__":
    pythoninterpreter = "python3.4"
    newcommand = [pythoninterpreter, "bots-engine.py"]
    subprocess.call(newcommand)

    botsinit.generalinit("config")
    botsinit.initenginelogging("engine")
    botsinit.connect()
    unittest.main()
    logging.shutdown()
    botsglobal.db.close()
Exemplo n.º 13
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()
Exemplo n.º 14
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')
Exemplo n.º 15
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)