load config in order, /etc/bistu_wifi then ~/.config...
This commit is contained in:
		
							parent
							
								
									4b4b86146c
								
							
						
					
					
						commit
						d83c78b6b3
					
				| 
						 | 
					@ -16,7 +16,7 @@ else:
 | 
				
			||||||
    XDG_CONFIG_HOME = os.path.join(os.environ['HOME'], '.config')
 | 
					    XDG_CONFIG_HOME = os.path.join(os.environ['HOME'], '.config')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
DEFAULT_HOST = '10.1.206.13'
 | 
					DEFAULT_HOST = '10.1.206.13'
 | 
				
			||||||
DEFAULT_CONFIG_LOCATION = os.path.join(XDG_CONFIG_HOME, 'bistu-wifi/auth.ini')
 | 
					DEFAULT_CONFIG_LOCATION = [ os.path.join(XDG_CONFIG_HOME, 'bistu-wifi/auth.ini'), '/etc/bistu-wifi/auth.ini' ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
| 
						 | 
					@ -39,17 +39,19 @@ def main():
 | 
				
			||||||
                        type=str, help='host to send login/logout request to, default use 10.1.206.13')
 | 
					                        type=str, help='host to send login/logout request to, default use 10.1.206.13')
 | 
				
			||||||
    parser.add_argument('-o', '--logout', action='store_true',
 | 
					    parser.add_argument('-o', '--logout', action='store_true',
 | 
				
			||||||
                        help='logout bistu wifi. try to login if not privided')
 | 
					                        help='logout bistu wifi. try to login if not privided')
 | 
				
			||||||
    parser.add_argument('-c', '--config', nargs='?', default=DEFAULT_CONFIG_LOCATION,
 | 
					    parser.add_argument('-c', '--config', nargs='?', default=None,
 | 
				
			||||||
                        type=str, help='load info(username, password, host) from config file. '
 | 
					                        type=str, help=f'load info(username, password, host) from config file.'
 | 
				
			||||||
                        f'Will try {DEFAULT_CONFIG_LOCATION} if not provided.')
 | 
					                        f'Will try {DEFAULT_CONFIG_LOCATION} if not provided.')
 | 
				
			||||||
    parser.add_argument('--notime', action='store_true',
 | 
					    parser.add_argument('--no-config', action='store_true',
 | 
				
			||||||
 | 
					                        help='don\'t use any config file')
 | 
				
			||||||
 | 
					    parser.add_argument('--no-time', action='store_true',
 | 
				
			||||||
                        help='don\'t output time field in log')
 | 
					                        help='don\'t output time field in log')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    args = parser.parse_args()
 | 
					    args = parser.parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    log_level = logging.DEBUG if 'BISTU_WIFI_DEBUG' in os.environ else logging.INFO
 | 
					    log_level = logging.DEBUG if 'BISTU_WIFI_DEBUG' in os.environ else logging.INFO
 | 
				
			||||||
    log_fmt = '%(levelname)s : %(funcName)s : %(message)s'
 | 
					    log_fmt = '%(levelname)s : %(funcName)s : %(message)s'
 | 
				
			||||||
    if not args.notime:
 | 
					    if not args.no_time:
 | 
				
			||||||
        log_fmt = '%(asctime)s : ' + log_fmt
 | 
					        log_fmt = '%(asctime)s : ' + log_fmt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    logging.basicConfig(level=log_level, format=log_fmt)
 | 
					    logging.basicConfig(level=log_level, format=log_fmt)
 | 
				
			||||||
| 
						 | 
					@ -59,11 +61,31 @@ def main():
 | 
				
			||||||
    host      = DEFAULT_HOST
 | 
					    host      = DEFAULT_HOST
 | 
				
			||||||
    is_logout = False
 | 
					    is_logout = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Load config from file
 | 
				
			||||||
 | 
					    #   Try files [ global_default, user_default ] if args.config not provided,
 | 
				
			||||||
 | 
					    #   Try file specified by user                 if args.config provided
 | 
				
			||||||
 | 
					    #
 | 
				
			||||||
 | 
					    # After these judgements, the config file must exist or is None
 | 
				
			||||||
 | 
					    config_file = None
 | 
				
			||||||
    if args.config is not None:
 | 
					    if args.config is not None:
 | 
				
			||||||
        logger.info('loading info from config file %s ...', args.config)
 | 
					        config_file = args.config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if not os.path.exists(config_file):
 | 
				
			||||||
 | 
					            logger.error('config file not exists: %s', config_file)
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        for f in DEFAULT_CONFIG_LOCATION:
 | 
				
			||||||
 | 
					            if os.path.exists(f):
 | 
				
			||||||
 | 
					                config_file = f
 | 
				
			||||||
 | 
					                break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if args.no_config:
 | 
				
			||||||
 | 
					        config_file = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # the config_file must exists now
 | 
				
			||||||
 | 
					    if config_file is not None:
 | 
				
			||||||
 | 
					        logger.info('loading info from config file %s ...', config_file)
 | 
				
			||||||
        config = configparser.ConfigParser()
 | 
					        config = configparser.ConfigParser()
 | 
				
			||||||
        config.read(args.config)
 | 
					        config.read(config_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        d_section = config['DEFAULT']
 | 
					        d_section = config['DEFAULT']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -71,6 +93,7 @@ def main():
 | 
				
			||||||
        password = str(d_section['password']) if 'password' in d_section else password
 | 
					        password = str(d_section['password']) if 'password' in d_section else password
 | 
				
			||||||
        host     = str(d_section['host'])     if 'host'     in d_section else host
 | 
					        host     = str(d_section['host'])     if 'host'     in d_section else host
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # override with command paramter
 | 
				
			||||||
    username = args.username if args.username is not None else username
 | 
					    username = args.username if args.username is not None else username
 | 
				
			||||||
    password = args.password if args.password is not None else password
 | 
					    password = args.password if args.password is not None else password
 | 
				
			||||||
    host     = args.host     if args.host     is not None else host
 | 
					    host     = args.host     if args.host     is not None else host
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue