1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-09-27 15:46:53 -04:00
aprsd/aprsd/utils.py
Walter A. Boring IV ce7a30aa78 Require ~/.aprsd/config.yml
This patch completes the migration to using a config.yml file.
~/.aprsd/config.yml is now required and all options for callsign,
imap, aprs user, passwords are in the config.  If there is no existing
~/.aprsd/config.yml file, then the app will output a sample config
and exit.

This patch also adds a global logging facility that allows logging all
commands to aprsd.log as well as stdout.  You can disable logging to
stdout by adding --quiet on the command line.  You can specify the log
level with --loglevel INFO.  By default the log level is DEBUG.

This patch also updates some formatting issues and small refactoring
to ensure that the logging facility and config is read prior to starting
any network connections and/or services.
2018-11-29 08:22:41 -05:00

56 lines
1.3 KiB
Python

"""Utilities and helper functions."""
import logging
import os
import sys
import yaml
# an example of what should be in the ~/.aprsd/config.yml
example_config = '''
ham:
callsign: KFART
aprs:
login: someusername
password: password
host: noam.aprs2.net
shortcuts:
'aa': '5551239999@vtext.com'
'cl': 'craiglamparter@somedomain.org'
'wb': '555309@vtext.com'
smtp:
login: something
password: some lame password
imap:
login: imapuser
password: something dumb
'''
log = logging.getLogger('APRSD')
def env(*vars, **kwargs):
"""This returns the first environment variable set.
if none are non-empty, defaults to '' or keyword arg default
"""
for v in vars:
value = os.environ.get(v, None)
if value:
return value
return kwargs.get('default', '')
def get_config():
"""This tries to read the yaml config from ~/.aprsd/config.yml."""
config_file = os.path.expanduser("~/.aprsd/config.yml")
if os.path.exists(config_file):
with open(config_file, "r") as stream:
config = yaml.load(stream)
return config
else:
log.critical("%s is missing, please create a config file" % config_file)
print("\nCopy to ~/.aprsd/config.yml and edit\n\nSample config:\n %s" % example_config)
sys.exit(-1)