Skip to content

Commit

Permalink
Support inclusion checks
Browse files Browse the repository at this point in the history
  • Loading branch information
cphyc committed Mar 29, 2021
1 parent 021c387 commit 2f03528
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
37 changes: 21 additions & 16 deletions yt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from more_itertools import always_iterable

from yt._maintenance.deprecation import issue_deprecation_warning
from yt.utilities.configuration_tree import ConfigNode
from yt.utilities.configuration_tree import ConfigLeaf, ConfigNode

ytcfg_defaults = {}

Expand Down Expand Up @@ -99,13 +99,15 @@ def __init__(self, defaults=None):
defaults = {}
self.config_root = ConfigNode(None)

def get(self, section, *keys, callback=None, **kwargs):
if callback is None:

def callback(leaf):
return leaf.value

return self.config_root.get_leaf(section, *keys, callback=callback)
def get(self, section, *keys, callback=None):
node_or_leaf = self.config_root.get(section, *keys)
if isinstance(node_or_leaf, ConfigLeaf):
if callback is None:
return node_or_leaf.value
else:
return callback(node_or_leaf)
else:
return node_or_leaf

def get_most_specific(self, section, *keys, **kwargs):
use_fallback = "fallback" in kwargs
Expand Down Expand Up @@ -148,14 +150,6 @@ def set(self, *args, metadata=None):
[section] + list(keys), value, extra_data=metadata
)

def __setitem__(self, args, value):
section, *keys = always_iterable(args)
self.set(section, *keys, value, metadata=None)

def __getitem__(self, key):
section, *keys = always_iterable(key)
return self.get(section, *keys)

def remove(self, *args):
self.config_root.pop_leaf(args)

Expand Down Expand Up @@ -190,6 +184,17 @@ def get_global_config_file():
def get_local_config_file():
return os.path.join(os.path.abspath(os.curdir), "yt.toml")

def __setitem__(self, args, value):
section, *keys = always_iterable(args)
self.set(section, *keys, value, metadata=None)

def __getitem__(self, key):
section, *keys = always_iterable(key)
return self.get(section, *keys)

def __contains__(self, item):
return item in self.config_root


_global_config_file = YTConfig.get_global_config_file()
_local_config_file = YTConfig.get_local_config_file()
Expand Down
9 changes: 6 additions & 3 deletions yt/utilities/configuration_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ def serialize(self):
retval[key] = child.serialize()
return retval

def __repr__(self):
return f"<Node {self.key}>"

@staticmethod
def from_dict(other, parent=None, **kwa):
me = ConfigNode(None, parent=parent)
Expand Down Expand Up @@ -137,6 +134,12 @@ def as_dict(self, callback=lambda child: child.value):
data, _ = self._as_dict_with_count(callback)
return data

def __repr__(self):
return f"<Node {self.key}>"

def __contains__(self, item):
return item in self.children


class ConfigLeaf:
def __init__(self, key, parent: ConfigNode, value, extra_data=None):
Expand Down

0 comments on commit 2f03528

Please sign in to comment.