Skip to content

Commit

Permalink
extracted selectors and cahnged
Browse files Browse the repository at this point in the history
  • Loading branch information
andras-tim committed Mar 23, 2016
1 parent be26881 commit 6518e6c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
24 changes: 12 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ Config format

An **octoconf** YAML file have two restricted keywords:

* ``USED_CONFIG: <node_name>`` in the file root
* ``USED_CONFIG>: <node_name>`` in the file root
you can specify the name of default config profile

* ``Base: <node_name>`` in the 2nd level
* ``<BASE: <node_name>`` in the 2nd level
this will used for making (merge based) inheritance between profiles

*The profile nodes should be on 1st level!*
Expand All @@ -43,31 +43,31 @@ There is an example YAML for demonstration

.. code-block:: yaml
USED_CONFIG: Fruits
USED_CONFIG>: Fruits
Fruits:
RED: 1
YELLOW: $VAR1/2
SmallFruits:
Base: Fruits
<BASE: Fruits
RED: 3
ExtraSmallFruits:
Base: SmallFruits
<BASE: SmallFruits
RED: 4
Base case
~~~~~~~~~
Basic case
~~~~~~~~~~

Read a multiple config contained YAML, and get profile which was selected by default.

* Example YAML:
.. code-block:: yaml
USED_CONFIG: Fruits
USED_CONFIG>: Fruits
Fruits:
Small:
Expand Down Expand Up @@ -106,7 +106,7 @@ Read a YAML file which contains variables.
* Example YAML:
.. code-block:: yaml
USED_CONFIG: Fruits
USED_CONFIG>: Fruits
Fruits:
Small:
Expand Down Expand Up @@ -145,7 +145,7 @@ Read a multiple config contained YAML, where the selected config is inherited fr
* Example YAML:
.. code-block:: yaml
USED_CONFIG: ExtraSmallFruits
USED_CONFIG>: ExtraSmallFruits
Fruits:
Small:
Expand All @@ -154,13 +154,13 @@ Read a multiple config contained YAML, where the selected config is inherited fr
GREEN: 3
SmallFruits:
Base: Fruits
<BASE: Fruits
Small:
RED: 4
YELLOW: 5
ExtraSmallFruits:
Base: SmallFruits
<BASE: SmallFruits
Small:
RED: 6
Expand Down
22 changes: 11 additions & 11 deletions examples/test_config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
USED_CONFIG: ProductionConfig
USED_CONFIG>: ProductionConfig

# This config YAML has been copied from a Flask based project for make real example.
# FYI: The unit tests uses this file, so this is a lieve, and working config.
Expand All @@ -25,44 +25,44 @@ DefaultConfig:

# This profile is only an alias for the "ProductionConfig"
UserConfig:
Base: ProductionConfig
<BASE: ProductionConfig


# This profile inherites all properties from the "DefaultConfig"
ProductionConfig:
Base: DefaultConfig
<BASE: DefaultConfig
DebugId: Production

SqlAlchemy:
SQLALCHEMY_DATABASE_URI: mysql+mysqlconnector://tasks:a@localhost/tasks


DevelopmentConfig:
Base: DefaultConfig
<BASE: DefaultConfig
DebugId: Development

Flask:
DEBUG: True


TestingConfig:
Base: DefaultConfig
<BASE: DefaultConfig
DebugId: Testing

Flask:
TESTING: True


DependencyTopConfig:
Base: DependencyMiddleConfig
<BASE: DependencyMiddleConfig
DebugId: DependencyTop

DependencyLevel:
1: Top


DependencyMiddleConfig:
Base: DependencyBottomConfig
<BASE: DependencyBottomConfig
DebugId: DependencyMiddle

DependencyLevel:
Expand All @@ -80,20 +80,20 @@ DependencyBottomConfig:


MinimalCircularConfig:
Base: MinimalCircularConfig
<BASE: MinimalCircularConfig
DebugId: MinimalCircular


MultiCircularConfigTop:
Base: MultiCircularConfigMiddle
<BASE: MultiCircularConfigMiddle
DebugId: MultiCircularTop


MultiCircularConfigMiddle:
Base: MultiCircularConfigBottom
<BASE: MultiCircularConfigBottom
DebugId: MultiCircularBottom


MultiCircularConfigBottom:
Base: MultiCircularConfigTop
<BASE: MultiCircularConfigTop
DebugId: MultiCircularBottom
12 changes: 8 additions & 4 deletions octoconf/octoconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from pprint import pformat


DEFAULT_CONFIG_SELECTOR = 'USED_CONFIG>'
BASE_CONFIG_SELECTOR = '<BASE'


class CircularDependencyError(Exception):
pass

Expand Down Expand Up @@ -80,7 +84,7 @@ def read(cls, yaml_path, variables=None, used_config=None, reader=None):
substituted_raw_yaml = cls.__substitute_yaml(raw_yaml, variables)
parsed_yaml = yaml.load(substituted_raw_yaml, Loader=loader)

used_config = used_config or parsed_yaml['USED_CONFIG']
used_config = used_config or parsed_yaml[DEFAULT_CONFIG_SELECTOR]
inherited_yaml = cls.__inherit_yaml(parsed_yaml, used_config)

return ConfigObject(inherited_yaml[used_config])
Expand Down Expand Up @@ -109,15 +113,15 @@ def __inherit_yaml(cls, parsed_yaml, config_name, parent_stack=None):
parent_stack.append(config_name)

# Has it base?
if 'Base' not in parsed_yaml[config_name].keys():
if BASE_CONFIG_SELECTOR not in parsed_yaml[config_name].keys():
return parsed_yaml

# Skipping circular-dependency
base_name = parsed_yaml[config_name]['Base']
base_name = parsed_yaml[config_name][BASE_CONFIG_SELECTOR]
if base_name in parent_stack:
raise CircularDependencyError('Circular dependency detected in YAML! ref_stack={!s}'.format(
str(parent_stack + [base_name])))
del parsed_yaml[config_name]['Base']
del parsed_yaml[config_name][BASE_CONFIG_SELECTOR]

# Get full config with inherited base config
parsed_yaml = cls.__inherit_yaml(parsed_yaml, base_name, parent_stack)
Expand Down
4 changes: 3 additions & 1 deletion tests/yaml_fake.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os

from octoconf.octoconf import DEFAULT_CONFIG_SELECTOR


def __read_fake_example_yaml(filename):
with open(os.path.join(os.path.dirname(__file__), '..', 'examples', filename)) as fd:
lines = fd.read().strip().splitlines()

lines[0] = 'USED_CONFIG: {used_config}'
lines[0] = '%s: {used_config}' % DEFAULT_CONFIG_SELECTOR
return '\n'.join(lines)


Expand Down

0 comments on commit 6518e6c

Please sign in to comment.