glusto.configurable module¶
All things configuration.
Note
Configurable is inherited by the Glusto class and not designed to be instantiated.
-
class
glusto.configurable.
Configurable
¶ Bases:
object
The class providing all things configuration.
-
config
= {}¶ The default class attribute for storing configurations.
-
static
store_config
(obj, filename, config_type=None, **kwargs)¶ - Writes an object to a file format.
- Automatically detects format based on filename extension.
Parameters: - obj (object) – The Python object to store in file.
- filename (str) – Filename for output of configuration.
- config_type (optional[str]) – The type of config file. Use when extension needs to differ from actual type. (e.g., .conf instead of .yml)
Returns: Nothing
Note
Uses custom GDumper class to strip Python object formatting. This is not a utility function for serialization.
-
static
load_config
(filename, config_type=None, **kwargs)¶ Reads a config from file. Defaults to yaml, but will detect other config formats based on filename extension. Currently reads yaml, json, ini, csv, and text files.
Parameters: - filename (str) – Filename of configuration to be read.
- config_type (optional[str]) – The type of config file. Use when extension needs to differ from actual type. (e.g., .conf instead of .yml)
- **kwargs (optional[dict]) – keyword arguments specific to formats
Returns: Dict of configuration items.
-
static
load_yaml_string
(yaml_string)¶ Reads a yaml formatted string into a dictionary
Parameters: yaml_string (str) – A string containing yaml formatted text. Returns: Dictionary on success.
-
static
load_json_string
(json_string)¶ Reads a json formatted string into a dictionary
Parameters: json_string (str) – A string containing json formatted text. Returns: Dictionary on success.
-
static
load_configs
(filelist)¶ Reads multiple configs from a list of filenames into a single configuration.
Parameters: filelist (list) – List of configuration filenames to read. Returns: Dict of configuration items.
-
classmethod
load_config_defaults
()¶
-
classmethod
set_config
(config)¶ Assigns a config to the config class attribute.
Parameters: config (dict) – A dictionary of configuration objects. Returns: Nothing Warning
DESTRUCTIVE. This will assign a new dictionary on top of an existing config. See update_config().
-
classmethod
update_config
(config)¶ Adds a config to the config class attribute.
Parameters: config (dict) – A dictionary of configuration objects. Returns: Nothing Warning
SOMEWHAT DESTRUCTIVE. This will overwrite any previously existing objects.
For example, config[‘thisandthat’] will overwrite cls.config[‘thisandthat’], but config[‘subconfig’][‘thisandthat’] will add the subconfig dictionary without overwriting cls.config[‘thisandthat’].
-
classmethod
log_config
(obj)¶ Writes a yaml formatted configuration to the log.
Parameters: obj (dict) – The configuration object to write to log. Returns: Nothing
-
static
get_config
(obj)¶ Retrieves an object in yaml format.
Parameters: obj (object) – A Python object to be converted to yaml. Returns: A yaml formatted string.
-
static
show_config
(obj)¶ Outputs a yaml formatted representation of an object on stdout.
Parameters: obj (object) – A Python object to be converted to yaml. Returns: Nothing
-
classmethod
clear_config
()¶ Clears the config class attribute with an empty dictionary.
Returns: Nothing
-
static
show_file
(filename)¶ Reads a file and prints the output.
Parameters: filename (str) – Name of the file to display. Returns: Nothing
-
-
class
glusto.configurable.
GDumper
(stream, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None)¶ Bases:
yaml.dumper.Dumper
Override the alias junk normally output by Dumper. This is necessary because PyYaml doesn’t give a simple option to modify the output and ignore tags, aliases, etc.
-
ignore_aliases
(data)¶ Overriding to skip aliases.
-
prepare_tag
(tag)¶ Overriding to skip tags. e.g., !!python/object:glusto.cluster.Cluster
-
-
class
glusto.configurable.
Intraconfig
¶ Bases:
object
Class to provide instances with simple configuration utility and introspection in yaml config format.
Intended to be inherited.
Example
To inherit Intraconfig in your custom class:
>>> from glusto.configurable import Intraconfig >>> class MyClass(Intraconfig): >>> def __init__(self): >>> self.myattribute = "this and that"
To use Intraconfig to output MyClass as yaml:
>>> myinstance = MyClass() >>> myinstance.show_config()
-
update_config
(config)¶ Adds a config to the config class attribute.
Parameters: config (dict) – A dictionary of configuration objects. Returns: Nothing Warning
SOMEWHAT DESTRUCTIVE. This will overwrite any previously existing objects.
For example, config[‘thisandthat’] will overwrite cls.config[‘thisandthat’], but config[‘subconfig’][‘thisandthat’] will add the subconfig dictionary without overwriting cls.config[‘thisandthat’].
-
show_config
()¶ Outputs a yaml formatted representation of an instance on stdout.
Returns: Nothing
-
get_config
()¶ Retrieves an instance object in yaml format.
Returns: A yaml formatted string.
-
load_config
(filename)¶ Reads a yaml config from file and assigns to the config instance attribute.
Parameters: filename (str) – Filename of configuration to be read. Returns: Nothing
-
store_config
(filename, config_type=None)¶ - Writes attributes of a class instance to a file in a config format.
- Automatically detects format based on filename extension.
Parameters: - filename (str) – Filename for output of configuration.
- config_type (optional[str]) – The type of config file. Use when extension needs to differ from actual type. (e.g., .conf instead of .yml)
Returns: Nothing
Note
Uses custom GDumper class to strip Python object formatting. This is not a utility function for serialization.
-