-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable configuration to control whether enable enable_dynamic_frequency
- Loading branch information
1 parent
97145ea
commit d09dcba
Showing
6 changed files
with
113 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
Syslog and daemon script utility library. | ||
""" | ||
|
||
from __future__ import print_function | ||
import json | ||
import logging | ||
import logging.config | ||
import sys | ||
from getopt import getopt | ||
|
||
|
||
# TODO: move to dbsync project. | ||
def usage(script_name): | ||
print('Usage: python ', script_name, | ||
'-t [host] -p [port] -s [unix_socket_path] -d [logging_level] -f [update_frequency] -h [help]') | ||
|
||
|
||
# TODO: move to dbsync project. | ||
def process_options(script_name): | ||
""" | ||
Process command line options | ||
""" | ||
options, remainders = getopt(sys.argv[1:], "t:p:s:d:f:rh", ["host=", "port=", "unix_socket_path=", "debug=", "frequency=", "enable_dynamic_frequency", "help"]) | ||
|
||
args = {} | ||
for (opt, arg) in options: | ||
try: | ||
if opt in ('-d', '--debug'): | ||
args['log_level'] = int(arg) | ||
elif opt in ('-t', '--host'): | ||
args['host'] = arg | ||
elif opt in ('-p', '--port'): | ||
args['port'] = int(arg) | ||
elif opt in ('-s', 'unix_socket_path'): | ||
args['unix_socket_path'] = arg | ||
elif opt in ('-f', '--frequency'): | ||
args['update_frequency'] = int(arg) | ||
elif opt in ('-r', '--enable_dynamic_frequency'): | ||
args['enable_dynamic_frequency'] = True | ||
elif opt in ('-h', '--help'): | ||
usage(script_name) | ||
sys.exit(0) | ||
except ValueError as e: | ||
print('Invalid option for {}: {}'.format(opt, e)) | ||
sys.exit(1) | ||
|
||
return args | ||
|
||
|
||
# TODO: move | ||
def setup_logging(config_file_path, log_level=logging.INFO): | ||
""" | ||
Logging configuration helper. | ||
:param config_file_path: file path to logging configuration file. | ||
https://docs.python.org/3/library/logging.config.html#object-connections | ||
:param log_level: defaults to logging.INFO | ||
:return: None - access the logger by name as described in the config--or the "root" logger as a backup. | ||
""" | ||
try: | ||
with open(config_file_path, 'rt') as f: | ||
config = json.load(f) | ||
logging.config.dictConfig(config) | ||
except (ValueError, IOError, OSError): | ||
# json.JSONDecodeError is throwable in Python3.5+ -- subclass of ValueError | ||
logging.basicConfig(log_level=log_level) | ||
logging.root.exception( | ||
"Could not load specified logging configuration '{}'. Verify the filepath exists and is compliant with: " | ||
"[https://docs.python.org/3/library/logging.config.html#object-connections]".format(config_file_path)) |