settings.ini: fix -c option handling

when argparse.FileType("r") used, variable passed is a open-ed file.

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
Hiroshi Miura
2021-05-23 15:16:38 +09:00
parent 74aaed0b59
commit fb088cac05
2 changed files with 16 additions and 10 deletions

View File

@@ -189,7 +189,7 @@ class Settings(object):
"_lock": multiprocessing.Lock(),
}
def __init__(self, config_path=None):
def __init__(self, file=None):
self.__dict__ = self._shared_state
if self.config is None:
with self._lock:
@@ -201,8 +201,15 @@ class Settings(object):
) as f:
self.config.read_file(f)
# load custom file
if config_path is not None:
self.config.read(config_path)
if file is not None:
if isinstance(file, str):
result = self.config.read(file)
if len(result) == 0:
raise IOError("Fails to load specified config file {}".format(file))
else:
# passed through command line argparse.FileType("r")
self.config.read_file(file)
file.close()
# load combinations
with open(
os.path.join(os.path.dirname(__file__), "combinations.json"),

View File

@@ -603,13 +603,11 @@ class Cli:
"-c",
"--config",
type=argparse.FileType("r"),
nargs=1,
help="Configuration ini file.",
)
parser.add_argument(
"--logging-conf",
type=argparse.FileType("r"),
nargs=1,
help="Logging configuration ini file.",
)
parser.add_argument("--logger", nargs=1, help="Specify logger name")
@@ -759,13 +757,14 @@ class Cli:
self.logger = logging.getLogger("aqt")
def _setup_settings(self, args=None, env_key="AQT_CONFIG"):
config = os.getenv(env_key, None)
if args is not None and args.config is not None:
config = args.config
if config is not None and os.path.exists(config):
self.settings = Settings(config)
self.settings = Settings(args.config)
else:
self.settings = Settings()
config = os.getenv(env_key, None)
if config is not None and os.path.exists(config):
self.settings = Settings(config)
else:
self.settings = Settings()
def run(self, arg=None):
args = self.parser.parse_args(arg)