From f8526c1a2fd93913be7c00fe8fa6a671dc7d4941 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Fri, 10 Aug 2018 17:26:35 -0700 Subject: [PATCH] fixing copybutton and maybe the integration bug --- docs/conf.py | 2 +- tljh/config.py | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 3601094..327e27e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -17,7 +17,7 @@ def setup(app): app.add_javascript("https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js") # Enable MathJax for Math -extensions = ['sphinx.ext.mathjax', 'sphinx_copybutton'] +extensions = ['sphinx.ext.mathjax', 'sphinx_copybutton.sphinx_copybutton'] # The master toctree document. master_doc = 'index' diff --git a/tljh/config.py b/tljh/config.py index d4d45d0..51169e1 100644 --- a/tljh/config.py +++ b/tljh/config.py @@ -19,6 +19,7 @@ import re import sys from ruamel.yaml import YAML +from ruamel.yaml.comments import CommentedMap, CommentedSeq yaml = YAML(typ='rt') @@ -50,13 +51,12 @@ def set_item_in_config(config, property_path, value): else: # If we are asked to create new non-leaf nodes, we will always make them dicts # This means setting is *destructive* - will replace whatever is down there! - if cur_path not in cur_part or not isinstance(cur_part[cur_path], dict): + if cur_path not in cur_part or not _is_dict(cur_part[cur_path]): cur_part[cur_path] = {} cur_part = cur_part[cur_path] return config_copy - def add_item_to_config(config, property_path, value): """ Add an item to a list in config. @@ -68,7 +68,7 @@ def add_item_to_config(config, property_path, value): for i, cur_path in enumerate(path_components): if i == len(path_components) - 1: # Final component, it must be a list and we append to it - if cur_path not in cur_part or not isinstance(cur_part[cur_path], list): + if cur_path not in cur_part or not _is_list(cur_part[cur_path]): cur_part[cur_path] = [] cur_part = cur_part[cur_path] @@ -76,7 +76,7 @@ def add_item_to_config(config, property_path, value): else: # If we are asked to create new non-leaf nodes, we will always make them dicts # This means setting is *destructive* - will replace whatever is down there! - if cur_path not in cur_part or not isinstance(cur_part[cur_path], dict): + if cur_path not in cur_part or not _is_dict(cur_part[cur_path]): cur_part[cur_path] = {} cur_part = cur_part[cur_path] @@ -94,12 +94,12 @@ def remove_item_from_config(config, property_path, value): for i, cur_path in enumerate(path_components): if i == len(path_components) - 1: # Final component, it must be a list and we append to it - if cur_path not in cur_part or not isinstance(cur_part[cur_path], list): + if cur_path not in cur_part or not _is_list(cur_part[cur_path]): raise ValueError(f'{property_path} is not a list') cur_part = cur_part[cur_path] cur_part.remove(value) else: - if cur_path not in cur_part or not isinstance(cur_part[cur_path], dict): + if cur_path not in cur_part or not _is_dict(cur_part[cur_path]): raise ValueError(f'{property_path} does not exist in config!') cur_part = cur_part[cur_path] @@ -209,6 +209,14 @@ def parse_value(value_str): return value_str +def _is_dict(item): + return isinstance(item, (dict, CommentedMap)) + + +def _is_list(item): + return isinstance(item, (list, CommentedSeq)) + + def main(argv=None): if argv is None: argv = sys.argv[1:]