fix(api): duplicate exception raising and close file
This commit is contained in:
parent
ad7686e8a7
commit
401808d7d1
|
@ -1,6 +1,4 @@
|
||||||
import configparser
|
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
|
|
||||||
from .utils import get_random_string, read_config_file
|
from .utils import get_random_string, read_config_file
|
||||||
|
|
||||||
|
@ -15,13 +13,7 @@ LIBRETIME_STATIC_ROOT = os.getenv(
|
||||||
"LIBRETIME_STATIC_ROOT",
|
"LIBRETIME_STATIC_ROOT",
|
||||||
"/usr/share/airtime/api",
|
"/usr/share/airtime/api",
|
||||||
)
|
)
|
||||||
|
CONFIG = read_config_file(LIBRETIME_CONFIG_FILEPATH)
|
||||||
try:
|
|
||||||
CONFIG = read_config_file(LIBRETIME_CONFIG_FILEPATH)
|
|
||||||
except IOError as e:
|
|
||||||
print(f"Unable to read config file {LIBRETIME_CONFIG_FILEPATH}", file=sys.stderr)
|
|
||||||
print(e, file=sys.stderr)
|
|
||||||
CONFIG = configparser.ConfigParser()
|
|
||||||
|
|
||||||
|
|
||||||
# Quick-start development settings - unsuitable for production
|
# Quick-start development settings - unsuitable for production
|
||||||
|
|
|
@ -1,23 +1,24 @@
|
||||||
import configparser
|
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import sys
|
import sys
|
||||||
|
from configparser import ConfigParser
|
||||||
|
|
||||||
|
|
||||||
def read_config_file(config_path):
|
def read_config_file(config_filepath):
|
||||||
"""Parse the application's config file located at config_path."""
|
"""Parse the application's config file located at config_path."""
|
||||||
config = configparser.ConfigParser()
|
config = ConfigParser()
|
||||||
try:
|
try:
|
||||||
config.readfp(open(config_path))
|
with open(config_filepath, encoding="utf-8") as config_file:
|
||||||
except IOError as e:
|
config.read_file(config_file)
|
||||||
|
except IOError as error:
|
||||||
print(
|
print(
|
||||||
"Failed to open config file at {}: {}".format(config_path, e.strerror),
|
f"Unable to read config file at {config_filepath}: {error.strerror}",
|
||||||
file=sys.stderr,
|
file=sys.stderr,
|
||||||
)
|
)
|
||||||
raise e
|
return ConfigParser()
|
||||||
except Exception as e:
|
except Exception as error:
|
||||||
print(e.strerror, file=sys.stderr)
|
print(error, file=sys.stderr)
|
||||||
raise e
|
raise error
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue