1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-12-20 16:41:13 -05:00

Added the ability to add comments to the config file

This patch adds a new add_config_comments() function in utils.py
that allows you to insert a comment string in a raw_yaml string
that's already been created from the yaml.dump() call.
This commit is contained in:
Hemna 2021-01-14 12:38:30 -05:00
parent 264b7536b4
commit cdde9c290b
2 changed files with 34 additions and 2 deletions

View File

@ -190,7 +190,7 @@ def setup_logging(config, loglevel, quiet):
@main.command()
def sample_config():
"""This dumps the config to stdout."""
click.echo(yaml.dump(utils.DEFAULT_CONFIG_DICT))
click.echo(utils.add_config_comments(yaml.dump(utils.DEFAULT_CONFIG_DICT)))
@main.command()

View File

@ -86,6 +86,37 @@ def mkdir_p(path):
raise
def insert_str(string, str_to_insert, index):
return string[:index] + str_to_insert + string[index:]
def end_substr(original, substr):
"""Get the index of the end of the <substr>.
So you can insert a string after <substr>
"""
idx = original.find(substr)
if idx != -1:
idx += len(substr)
return idx
def add_config_comments(raw_yaml):
end_idx = end_substr(raw_yaml, "aprs.fi:")
print("PENIS!!!!")
if end_idx != -1:
# lets insert a comment
print("Didn't find shit!")
raw_yaml = insert_str(
raw_yaml,
"\n # Get the apiKey from your aprs.fi account here: http://aprs.fi/account",
end_idx,
)
print(raw_yaml)
return raw_yaml
def create_default_config():
"""Create a default config file."""
# make sure the directory location exists
@ -95,7 +126,8 @@ def create_default_config():
click.echo("Config dir '{}' doesn't exist, creating.".format(config_dir))
mkdir_p(config_dir)
with open(config_file_expanded, "w+") as cf:
yaml.dump(DEFAULT_CONFIG_DICT, cf)
raw_yaml = yaml.dump(DEFAULT_CONFIG_DICT)
cf.write(add_config_comments(raw_yaml))
def get_config(config_file):