in projects/imagen-voice-captioning/imagen-voice-captioning.py [0:0]
def parse_config_args(config_file):
"""Parses config.ini and command line args.
Parses first the .ini -style config file, and then command line args.
CLI args overwrite default values from the config file.
Args:
config_file: path to the config.ini
Returns:
Configparser config
Raises:
None
"""
# Create a config parser object
config = configparser.ConfigParser()
config["DEFAULT"] = {"input": 0, "credentials": "credentials.json"}
config["parameters"] = {}
# Create an argument parser
parser = argparse.ArgumentParser()
# Read the configuration file
read_config = config.read(config_file)
if not read_config:
print("{} not found. Using command line args only".format(config_file))
# Add arguments for each configuration value using hardcoded defaults
parser.add_argument(
"--input",
default=config["DEFAULT"]["input"],
type=str,
help="Camera device number",
)
parser.add_argument(
"--credentials",
default=config["DEFAULT"]["credentials"],
type=str,
help=(
"Google Cloud Service account JSON key. "
"Default: ./credentials.json"
),
)
parser.add_argument(
"--project_id",
required=True,
type=str,
help="Google Cloud Project ID string",
)
else:
# Add arguments for each configuration value using read file
# for fallback defaults
parser.add_argument(
"--input",
default=config["parameters"]["input"],
type=str,
help="Camera device number",
)
parser.add_argument(
"--credentials",
default=config["parameters"]["credentials"],
type=str,
help=(
"Google Cloud Service account JSON key. "
"Default: ./credentials.json"
),
)
parser.add_argument(
"--project_id",
default=config["parameters"]["project_id"],
type=str,
help="Google Cloud Project ID string",
)
# Parse the arguments
args = parser.parse_args()
# Update the configuration values with the command line arguments
for arg in vars(args):
config["parameters"][arg] = getattr(args, arg)
print(dict(config["parameters"]))
# Check for required values
if not config["parameters"]["project_id"]:
print("error: the following arguments are required: --project_id")
exit(1)
return config