-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create a python implementation of get-json-settings
- this will allow for this functionality to be used in situations where the Perl interpreter is not available
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/python3 | ||
|
||
import argparse | ||
import logging | ||
|
||
import sys | ||
import os | ||
from pathlib import Path | ||
TOOLBOX_HOME = os.environ.get('TOOLBOX_HOME') | ||
if TOOLBOX_HOME is None: | ||
print("This script requires libraries that are provided by the toolbox project.") | ||
print("Toolbox can be acquired from https://github.com/perftool-incubator/toolbox and") | ||
print("then use 'export TOOLBOX_HOME=/path/to/toolbox' so that it can be located.") | ||
exit(1) | ||
else: | ||
p = Path(TOOLBOX_HOME) / 'python' | ||
if not p.exists() or not p.is_dir(): | ||
print("ERROR: <TOOLBOX_HOME>/python ('%s') does not exist!" % (p)) | ||
exit(2) | ||
sys.path.append(str(p)) | ||
from toolbox.json import * | ||
from toolbox.jsonsettings import * | ||
|
||
def process_options(): | ||
parser = argparse.ArgumentParser(description="Extract a settings value from a JSON config file") | ||
|
||
parser.add_argument("--log-level", | ||
dest = "log_level", | ||
help = "Control how much logging output should be generated", | ||
default = "normal", | ||
choices = [ "normal", "verbose", "debug" ]) | ||
|
||
parser.add_argument("--settings-file", | ||
dest = "settings_file", | ||
help = "Which file should be loaded to extract a value from", | ||
required = True, | ||
type = str) | ||
|
||
parser.add_argument("--query", | ||
dest = "query", | ||
help = "The query to run to find the desired settings property", | ||
required = True, | ||
type = str) | ||
|
||
the_args = parser.parse_args() | ||
|
||
return the_args | ||
|
||
|
||
def main(): | ||
log_debug_format = '[%(asctime)s %(levelname)s %(module)s %(funcName)s:%(lineno)d] %(message)s' | ||
log_verbose_format = '[%(asctime)s %(levelname)s] %(message)s' | ||
log_normal_format = '%(message)s' | ||
|
||
if args.log_level == 'debug': | ||
logging.basicConfig(level = logging.DEBUG, format = log_debug_format, stream = sys.stderr) | ||
elif args.log_level == 'verbose': | ||
logging.basicConfig(level = logging.INFO, format = log_verbose_format, stream = sys.stderr) | ||
elif args.log_level == 'normal': | ||
logging.basicConfig(level = logging.INFO, format = log_normal_format, stream = sys.stderr) | ||
|
||
logger = logging.getLogger(__file__) | ||
|
||
settings,err_msg = load_json_file(args.settings_file) | ||
if err_msg is not None: | ||
logger.error(f"ERROR: failed to load JSON settings from {args.settings_file}") | ||
logger.error(f" Reason is: {err_msg}") | ||
return 1 | ||
else: | ||
logger.info(f"Loaded JSON settings from {args.settings_file}") | ||
|
||
value,error_no = get_json_setting(settings, args.query) | ||
if error_no != 0: | ||
logger.error(f"ERROR: JSON query '{args.query}' failed") | ||
return 1 | ||
else: | ||
logger.info(f"JSON query '{args.query}' returned '{value}'") | ||
|
||
print(value); | ||
|
||
return 0 | ||
|
||
if __name__ == "__main__": | ||
args = process_options() | ||
logger = None | ||
|
||
exit(main()) |
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,51 @@ | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__file__) | ||
|
||
def get_json_setting(settings_ref, query): | ||
# break the query into fields to attempt to use to traverse the | ||
# settings | ||
query_fields = query.split(".") | ||
logger.debug(f"query_fields = {query_fields}") | ||
|
||
query_rc = 0 | ||
query_return = None | ||
|
||
# iterate through all supplied query fields | ||
for field_idx,field in enumerate(query_fields): | ||
logger.debug(f"field_idx={field_idx} field={field}") | ||
logger.debug(f"settings_ref={settings_ref}") | ||
|
||
if field in settings_ref: | ||
logger.debug(f"found {field} in settings_ref") | ||
|
||
if isinstance(settings_ref[field], dict): | ||
logger.debug(f"found a dict at settings_ref[{field}]") | ||
logger.debug(f"settings_ref[{field}]={settings_ref[field]}") | ||
|
||
if field_idx == (len(query_fields) - 1): | ||
logger.debug(f"the found field is a dict but there are no more fields to search for, failing") | ||
query_rc = 1 | ||
break | ||
else: | ||
logger.debug(f"narrowing the scope to continue searching") | ||
settings_ref = settings_ref[field] | ||
else: | ||
logger.debug(f"found something besides a dict at settings_ref[{field}]") | ||
logger.debug(f"settings_ref[{field}]={settings_ref[field]}") | ||
|
||
if field_idx == (len(query_fields) - 1): | ||
logger.debug(f"this was the last query field and it does not point to a dict so assume is the desired value") | ||
query_return = settings_ref[field] | ||
logger.debug(f"query_return={query_return}") | ||
else: | ||
logger.debug(f"this was not the last query field and there is no further to search, failing") | ||
query_rc = 1 | ||
break | ||
else: | ||
logger.debug(f"did not find {field} in settings_ref") | ||
query_rc = 1 | ||
break | ||
|
||
return query_return,query_rc |