-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
102 additions
and
6 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
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
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
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
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,10 @@ | ||
import yaml | ||
from .py_config import PyConfig | ||
|
||
|
||
class YamlConfig(PyConfig): | ||
|
||
def load(self, config_file): | ||
with open(config_file) as config: | ||
config_obj = yaml.load(config) | ||
self._add_section_recursive(config_obj, self.sections['root']) |
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,2 @@ | ||
certifi==2018.1.18 | ||
PyYAML==3.12 |
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
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,20 @@ | ||
path: some_path | ||
hosts: ["example.com", "http://bing.com", "ssh.com:23", "www.qwe.asd"] | ||
|
||
section: { | ||
"attr1": 7.1, | ||
"attr2": 42, | ||
"foo": 123 | ||
} | ||
|
||
section2: { | ||
"inner_section": { | ||
"test_section": { | ||
1: 2 | ||
}, | ||
"two": 2, | ||
}, | ||
"attr1": 7.1, | ||
"attr2": 42, | ||
"foo": 123 | ||
} |
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,47 @@ | ||
import unittest | ||
|
||
from pyconf import api, YamlConfig, Section | ||
from typing import Sequence | ||
|
||
|
||
class TestPyConfig(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.sample_config_filename = 'tests/sample.yml' | ||
self.missing_config_filename = 'missing.yml' | ||
|
||
def test_load_general(self): | ||
"""Check if a valid config file is parsed correctly""" | ||
|
||
self.assertIsInstance(api.load(self.sample_config_filename, config_class=YamlConfig), YamlConfig) | ||
|
||
def test_load_missing_file(self): | ||
"""Check if the correct exception in raised when a missing config file is attempted to parse""" | ||
|
||
with self.assertRaises(FileNotFoundError): | ||
api.load(self.missing_config_filename) | ||
|
||
def test_load_check_types(self): | ||
"""Check automatic float, integer, and boolean value conversion""" | ||
config_data = api.load(self.sample_config_filename, config_class=YamlConfig) | ||
self.assertIsInstance(config_data['path'], str) | ||
self.assertIsInstance(config_data['hosts'], Sequence) | ||
self.assertIsInstance(config_data['hosts'][0], str) | ||
|
||
self.assertIsInstance(config_data['section'], Section) | ||
self.assertIsInstance(config_data['section']['attr1'], float) | ||
self.assertIsInstance(config_data['section']['attr2'], int) | ||
self.assertIsInstance(config_data['section']['foo'], int) | ||
|
||
self.assertIsInstance(config_data['section2'], Section) | ||
self.assertIsInstance(config_data['section2']['attr1'], float) | ||
self.assertIsInstance(config_data['section2']['attr2'], int) | ||
self.assertIsInstance(config_data['section2']['foo'], int) | ||
self.assertIsInstance(config_data['section2']['inner_section'], Section) | ||
self.assertIsInstance(config_data['section2']['inner_section']['two'], int) | ||
self.assertIsInstance(config_data['section2']['inner_section']['test_section'], Section) | ||
self.assertIsInstance(config_data['section2']['inner_section']['test_section'][1], int) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |