2018-11-21 15:55:14 -05:00
|
|
|
"""Utilities and helper functions."""
|
|
|
|
|
2018-11-29 08:22:41 -05:00
|
|
|
import logging
|
2018-11-21 15:55:14 -05:00
|
|
|
import os
|
|
|
|
import sys
|
2018-11-21 18:20:11 -05:00
|
|
|
import yaml
|
2018-11-21 15:55:14 -05:00
|
|
|
|
2018-11-21 18:20:11 -05:00
|
|
|
# an example of what should be in the ~/.aprsd/config.yml
|
|
|
|
example_config = '''
|
2018-11-29 08:22:41 -05:00
|
|
|
ham:
|
|
|
|
callsign: KFART
|
|
|
|
|
|
|
|
aprs:
|
|
|
|
login: someusername
|
|
|
|
password: password
|
|
|
|
host: noam.aprs2.net
|
2018-11-29 09:20:15 -05:00
|
|
|
port: 14580
|
2018-11-29 14:19:52 -05:00
|
|
|
logfile: /tmp/aprsd.log
|
2018-11-29 08:22:41 -05:00
|
|
|
|
2018-11-21 18:20:11 -05:00
|
|
|
shortcuts:
|
|
|
|
'aa': '5551239999@vtext.com'
|
|
|
|
'cl': 'craiglamparter@somedomain.org'
|
|
|
|
'wb': '555309@vtext.com'
|
|
|
|
|
|
|
|
smtp:
|
|
|
|
login: something
|
|
|
|
password: some lame password
|
2018-11-29 14:19:52 -05:00
|
|
|
host: imap.gmail.com
|
|
|
|
port: 465
|
2018-11-21 18:20:11 -05:00
|
|
|
|
|
|
|
imap:
|
|
|
|
login: imapuser
|
|
|
|
password: something dumb
|
2018-11-29 14:19:52 -05:00
|
|
|
host: imap.gmail.com
|
2018-11-21 18:20:11 -05:00
|
|
|
'''
|
2018-11-21 15:55:14 -05:00
|
|
|
|
2018-11-29 08:22:41 -05:00
|
|
|
log = logging.getLogger('APRSD')
|
|
|
|
|
2019-05-15 16:12:59 -04:00
|
|
|
|
2018-11-21 15:55:14 -05:00
|
|
|
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', '')
|
2018-11-21 18:20:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2020-12-04 08:56:26 -05:00
|
|
|
config = yaml.load(stream, Loader=yaml.FullLoader)
|
2018-11-21 18:20:11 -05:00
|
|
|
return config
|
|
|
|
else:
|
2019-05-15 16:12:59 -04:00
|
|
|
log.critical("%s is missing, please create config file" % config_file)
|
|
|
|
print("\nCopy to ~/.aprsd/config.yml and edit\n\nSample config:\n %s"
|
|
|
|
% example_config)
|
2018-11-21 18:23:06 -05:00
|
|
|
sys.exit(-1)
|
2018-11-29 13:20:53 -05:00
|
|
|
|
2019-05-15 16:12:59 -04:00
|
|
|
|
2018-11-29 13:20:53 -05:00
|
|
|
# This method tries to parse the config yaml file
|
|
|
|
# and consume the settings.
|
|
|
|
# If the required params don't exist,
|
|
|
|
# it will look in the environment
|
2020-12-09 08:54:17 -05:00
|
|
|
def parse_config():
|
2018-11-29 13:20:53 -05:00
|
|
|
# for now we still use globals....ugh
|
|
|
|
global CONFIG, LOG
|
|
|
|
|
|
|
|
def fail(msg):
|
|
|
|
LOG.critical(msg)
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2018-11-29 14:19:52 -05:00
|
|
|
def check_option(config, section, name=None, default=None):
|
2018-11-29 13:20:53 -05:00
|
|
|
if section in config:
|
|
|
|
if name and name not in config[section]:
|
2018-11-29 14:19:52 -05:00
|
|
|
if not default:
|
|
|
|
fail("'%s' was not in '%s' section of config file" %
|
|
|
|
(name, section))
|
|
|
|
else:
|
|
|
|
config[section][name] = default
|
2018-11-29 13:20:53 -05:00
|
|
|
else:
|
|
|
|
fail("'%s' section wasn't in config file" % section)
|
2018-11-29 14:19:52 -05:00
|
|
|
return config
|
2018-11-29 13:20:53 -05:00
|
|
|
|
|
|
|
# Now read the ~/.aprds/config.yml
|
|
|
|
config = get_config()
|
|
|
|
check_option(config, 'shortcuts')
|
|
|
|
check_option(config, 'ham', 'callsign')
|
|
|
|
check_option(config, 'aprs', 'login')
|
|
|
|
check_option(config, 'aprs', 'password')
|
|
|
|
check_option(config, 'aprs', 'host')
|
|
|
|
check_option(config, 'aprs', 'port')
|
2018-11-29 14:19:52 -05:00
|
|
|
config = check_option(config, 'aprs', 'logfile', './aprsd.log')
|
2018-11-29 13:20:53 -05:00
|
|
|
check_option(config, 'imap', 'host')
|
|
|
|
check_option(config, 'imap', 'login')
|
|
|
|
check_option(config, 'imap', 'password')
|
2018-11-29 14:19:52 -05:00
|
|
|
check_option(config, 'smtp', 'host')
|
|
|
|
check_option(config, 'smtp', 'port')
|
|
|
|
check_option(config, 'smtp', 'login')
|
|
|
|
check_option(config, 'smtp', 'password')
|
2018-11-29 13:20:53 -05:00
|
|
|
|
|
|
|
return config
|
|
|
|
LOG.info("aprsd config loaded")
|