|
@@ -9,7 +9,7 @@ INDENT = 4
|
|
|
|
|
|
def _insert_newline_before_comment(config, field_name):
|
|
|
'''
|
|
|
- Using some ruamel.yaml black magic, insert a blank line in the config right befor the given
|
|
|
+ Using some ruamel.yaml black magic, insert a blank line in the config right before the given
|
|
|
field and its comments.
|
|
|
'''
|
|
|
config.ca.items[field_name][1].insert(
|
|
@@ -40,10 +40,58 @@ def _schema_to_sample_configuration(schema, level=0):
|
|
|
return config
|
|
|
|
|
|
|
|
|
-def write_configuration(config_filename, config, mode=0o600):
|
|
|
+def _comment_out_line(line):
|
|
|
+ # If it's already is commented out (or empty), there's nothing further to do!
|
|
|
+ stripped_line = line.lstrip()
|
|
|
+ if not stripped_line or stripped_line.startswith('#'):
|
|
|
+ return line
|
|
|
+
|
|
|
+ # Comment out the names of optional sections.
|
|
|
+ one_indent = ' ' * INDENT
|
|
|
+ if not line.startswith(one_indent):
|
|
|
+ return '#' + line
|
|
|
+
|
|
|
+ # Otherwise, comment out the line, but insert the "#" after the first indent for aesthetics.
|
|
|
+ return '#'.join((one_indent, line[INDENT:]))
|
|
|
+
|
|
|
+
|
|
|
+def _comment_out_optional_configuration(rendered_config):
|
|
|
'''
|
|
|
- Given a target config filename and a config data structure of nested OrderedDicts, write out the
|
|
|
- config to file as YAML. Create any containing directories as needed.
|
|
|
+ Post-process a rendered configuration string to comment out optional key/values. The idea is
|
|
|
+ that this prevents the user from having to comment out a bunch of configuration they don't care
|
|
|
+ about to get to a minimal viable configuration file.
|
|
|
+
|
|
|
+ Ideally ruamel.yaml would support this during configuration generation, but it's not terribly
|
|
|
+ easy to accomplish that way.
|
|
|
+ '''
|
|
|
+ lines = []
|
|
|
+ required = False
|
|
|
+
|
|
|
+ for line in rendered_config.split('\n'):
|
|
|
+ # Upon encountering a required configuration option, skip commenting out lines until the
|
|
|
+ # next blank line.
|
|
|
+ stripped_line = line.strip()
|
|
|
+ if stripped_line in {'source_directories:', 'repositories:'} or line == 'location:':
|
|
|
+ required = True
|
|
|
+ elif not stripped_line:
|
|
|
+ required = False
|
|
|
+
|
|
|
+ lines.append(_comment_out_line(line) if not required else line)
|
|
|
+
|
|
|
+ return '\n'.join(lines)
|
|
|
+
|
|
|
+
|
|
|
+def _render_configuration(config):
|
|
|
+ '''
|
|
|
+ Given a config data structure of nested OrderedDicts, render the config as YAML and return it.
|
|
|
+ '''
|
|
|
+ return yaml.round_trip_dump(config, indent=INDENT, block_seq_indent=INDENT)
|
|
|
+
|
|
|
+
|
|
|
+def write_configuration(config_filename, rendered_config, mode=0o600):
|
|
|
+ '''
|
|
|
+ Given a target config filename and rendered config YAML, write it out to file. Create any
|
|
|
+ containing directories as needed.
|
|
|
'''
|
|
|
if os.path.exists(config_filename):
|
|
|
raise FileExistsError('{} already exists. Aborting.'.format(config_filename))
|
|
@@ -54,7 +102,7 @@ def write_configuration(config_filename, config, mode=0o600):
|
|
|
pass
|
|
|
|
|
|
with open(config_filename, 'w') as config_file:
|
|
|
- config_file.write(yaml.round_trip_dump(config, indent=INDENT, block_seq_indent=INDENT))
|
|
|
+ config_file.write(rendered_config)
|
|
|
|
|
|
os.chmod(config_filename, mode)
|
|
|
|
|
@@ -90,4 +138,7 @@ def generate_sample_configuration(config_filename, schema_filename):
|
|
|
schema = yaml.round_trip_load(open(schema_filename))
|
|
|
config = _schema_to_sample_configuration(schema)
|
|
|
|
|
|
- write_configuration(config_filename, config)
|
|
|
+ write_configuration(
|
|
|
+ config_filename,
|
|
|
+ _comment_out_optional_configuration(_render_configuration(config))
|
|
|
+ )
|