def test_as_args(self): config = Config(self.file_args) args = config.as_args(strip_prefixes=['circus'], omit_sections=['bleh', 'mi', 'floats'], omit_options=[('other', 'thing')]) wanted = [ '--other-stuff', '10.3', '--httpd', '--zmq-endpoint', 'http://ok' ] wanted.sort() args.sort() self.assertEqual(args, wanted) args = config.as_args(omit_sections=['bleh', 'mi', 'floats']) wanted = [ '--circus-zmq-endpoint', 'http://ok', '--other-thing', 'bleh', '--other-stuff', '10.3', '--circus-httpd' ] wanted.sort() args.sort() self.assertEqual(args, wanted) # it also works with an argparse parser parser = argparse.ArgumentParser(description='Run some watchers.') parser.add_argument('config', help='configuration file', nargs='?') parser.add_argument('-L', '--log-level', dest='loglevel') parser.add_argument('--log-output', dest='logoutput') parser.add_argument('--daemon', dest='daemonize', action='store_true') parser.add_argument('--pidfile', dest='pidfile') parser.add_argument('--multi', action='append') args = config.scan_args(parser, strip_prefixes=['mi']) args.sort() wanted = [ '--log-level', u'DEBUG', '--log-output', u'stdout', '--daemon', '--pidfile', u'pid', '--multi', 'one', '--multi', 'two', '--multi', 'three' ] wanted.sort() self.assertEqual(wanted, args)
def conf_parser(): if not os.path.exists('config.ini'): file = open('config.ini', 'w') file.write( '[Srv_settings]\n' f'SERVER_ADDRESS={str(socket.gethostbyname_ex(socket.gethostname())[2][0])}\n' 'SERVER_PORT=\n' 'PROTOCOL=TPTP\n' 'HEX_DUMP=0\n' 'PARSE=0\n\n' '[Tptp_settings]\n' 'STATUS=1\n' 'CLOSE_BATCH=1\n' 'PROCESS_TIME=0\n' 'REFUND_RC=001\n' 'VOID_RC=001\n\n' '[Own_settings]\n' 'KEY_DECLINE_RC=01\n' 'CLOSE_BATCH=1\n' 'PROCESS_TIME=0\n' 'REFUND_RC=00\n' 'SALE_RC=00\n\n' '[DELAY]\n' 'VOID_DELAY=0\n' 'FINAL_DELAY=0\n' 'STATUS_DELAY=0\n' 'REFUND_DELAY=0\n' 'QR_CODE_DELAY=0\n' 'DEFAULT_DELAY=0\n' 'CLOSE_OP_DELAY=0\n') file.close() conf = Config('config.ini') try: sett_dict = { 'parse': conf.as_args()[conf.as_args().index('--Srv-settings-PARSE') + 1], 'hexd': conf.as_args()[conf.as_args().index('--Srv-settings-HEX-DUMP') + 1], 'port': conf.as_args()[conf.as_args().index('--Srv-settings-SERVER-PORT') + 1], 'protocol': conf.as_args()[conf.as_args().index('--Srv-settings-PROTOCOL') + 1], 'addr': conf.as_args()[ conf.as_args().index('--Srv-settings-SERVER-ADDRESS') + 1], 'tptp_stat_time': conf.as_args()[conf.as_args().index('--Tptp-settings-PROCESS-TIME') + 1], 'tptp_cl_batch': conf.as_args()[conf.as_args().index('--Tptp-settings-CLOSE-BATCH') + 1], 'refund_rc': conf.as_args()[conf.as_args().index('--Tptp-settings-REFUND-RC') + 1], 'void_rc': conf.as_args()[conf.as_args().index('--Tptp-settings-VOID-RC') + 1], 'stat': conf.as_args()[conf.as_args().index('--Tptp-settings-STATUS') + 1], 'key_decline_rc': conf.as_args()[ conf.as_args().index('--Own-settings-KEY-DECLINE-RC') + 1], 'own_stat_time': conf.as_args()[conf.as_args().index('--Own-settings-PROCESS-TIME') + 1], 'own_cl_batch': conf.as_args()[conf.as_args().index('--Own-settings-CLOSE-BATCH') + 1], 'own_refund_rc': conf.as_args()[conf.as_args().index('--Own-settings-REFUND-RC') + 1], 'own_sale_rc': conf.as_args()[conf.as_args().index('--Own-settings-SALE-RC') + 1], 'void_time': conf.as_args()[conf.as_args().index('--DELAY-VOID-DELAY') + 1], 'final_time': conf.as_args()[conf.as_args().index('--DELAY-FINAL-DELAY') + 1], 'status_time': conf.as_args()[conf.as_args().index('--DELAY-STATUS-DELAY') + 1], 'refund_time': conf.as_args()[conf.as_args().index('--DELAY-REFUND-DELAY') + 1], 'qr_code_time': conf.as_args()[conf.as_args().index('--DELAY-QR-CODE-DELAY') + 1], 'default_time': conf.as_args()[conf.as_args().index('--DELAY-DEFAULT-DELAY') + 1], 'close_op_time': conf.as_args()[conf.as_args().index('--DELAY-CLOSE-OP-DELAY') + 1] } set_logging() logging.info(f'Config values:{sett_dict}') return sett_dict except ValueError: print('config.ini is damaged!') input('Press ENTER to exit.') sys.exit()