added showconfig in yaml

This commit is contained in:
Adolfo Gómez García 2022-09-16 22:34:40 +02:00
parent 4ec8841a57
commit 0d1d38c18a
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23

View File

@ -33,6 +33,7 @@
import logging import logging
import typing import typing
import csv import csv
import yaml
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from uds.core.util import config from uds.core.util import config
@ -51,6 +52,13 @@ class Command(BaseCommand):
default=False, default=False,
help='Shows configuration in CVS format', help='Shows configuration in CVS format',
) )
parser.add_argument(
'--yaml',
action='store_true',
dest='yaml',
default=False,
help='Shows configuration in YAML format',
)
def handle(self, *args, **options): def handle(self, *args, **options):
logger.debug("Show settings") logger.debug("Show settings")
@ -61,17 +69,26 @@ class Command(BaseCommand):
# Print header # Print header
writer = csv.writer(self.stdout, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) writer = csv.writer(self.stdout, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['Section', 'Name', 'Value']) writer.writerow(['Section', 'Name', 'Value'])
elif options['yaml']:
writer = {} # Create a dict to store data, and write at the end
# Get sections, key, value as a list of tuples # Get sections, key, value as a list of tuples
for section, data in config.Config.getConfigValues().items(): for section, data in config.Config.getConfigValues().items():
for key, value in data.items(): for key, value in data.items():
# value is a dict, get 'value' key # value is a dict, get 'value' key
if options['csv']: if options['csv']:
writer.writerow([section, key, value['value']]) writer.writerow([section, key, value['value']])
elif options['yaml']:
if section not in writer:
writer[section] = {}
writer[section][key] = value['value']
else: else:
v = value['value'].replace('\n', '\\n') v = value['value'].replace('\n', '\\n')
self.stdout.write(f'{section}.{key}="{v}"') self.stdout.write(f'{section}.{key}="{v}"')
if options['csv']:
writer.writerow([])
elif options['yaml']:
self.stdout.write(yaml.safe_dump(writer, default_flow_style=False))
except Exception as e: except Exception as e:
print('The command could not be processed: {}'.format(e)) print('The command could not be processed: {}'.format(e))
logger.exception('Exception processing %s', args) logger.exception('Exception processing %s', args)