Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow specifying directories as config paths #249

Merged
merged 2 commits into from
Aug 25, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def load_config(cls, description, argv, generate_section=None):
"-c", "--config-path",
action="append",
metavar="CONFIG_FILE",
help="Specify config file"
help="Specify config file. Can be given multiple times and"
" may specify directories containing *.yaml files."
)
config_parser.add_argument(
"--generate-config",
Expand All @@ -151,14 +152,41 @@ def load_config(cls, description, argv, generate_section=None):

generate_keys = config_args.generate_keys

config_files = []
if config_args.config_path:
for config_path in config_args.config_path:
if os.path.isdir(config_path):
# We accept specifying directories as config paths, we search
# inside that directory for all files matching *.yaml, and then
# we apply them in *sorted* order.
files = []
for entry in os.listdir(config_path):
entry_path = os.path.join(config_path, entry)
if not os.path.isfile(entry_path):
print (
"Found subdirectory in config directory: %r. IGNORING."
) % (entry_path, )
continue

if not entry.endswith(".yaml"):
print (
"Found file in config directory that does not"
" end in '.yaml': %r. IGNORING."
) % (entry_path, )
continue

config_files.extend(sorted(files))
else:
config_files.append(config_path)

if config_args.generate_config:
if not config_args.config_path:
if not config_files:
config_parser.error(
"Must supply a config file.\nA config file can be automatically"
" generated using \"--generate-config -H SERVER_NAME"
" -c CONFIG-FILE\""
)
(config_path,) = config_args.config_path
(config_path,) = config_files
if not os.path.exists(config_path):
config_dir_path = os.path.dirname(config_path)
config_dir_path = os.path.abspath(config_dir_path)
Expand Down Expand Up @@ -202,7 +230,7 @@ def load_config(cls, description, argv, generate_section=None):
obj.invoke_all("add_arguments", parser)
args = parser.parse_args(remaining_args)

if not config_args.config_path:
if not config_files:
config_parser.error(
"Must supply a config file.\nA config file can be automatically"
" generated using \"--generate-config -H SERVER_NAME"
Expand All @@ -213,8 +241,8 @@ def load_config(cls, description, argv, generate_section=None):
config_dir_path = os.path.abspath(config_dir_path)

specified_config = {}
for config_path in config_args.config_path:
yaml_config = cls.read_config_file(config_path)
for config_file in config_files:
yaml_config = cls.read_config_file(config_file)
specified_config.update(yaml_config)

server_name = specified_config["server_name"]
Expand Down