Skip to content

Commit 6381da5

Browse files
Merge pull request #5 from BrunoSilvaAndrade/develop
Develop
2 parents fed86fd + 0b975e2 commit 6381da5

5 files changed

Lines changed: 46 additions & 37 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ venv
44
dist
55
build
66
wheel
7+
.coverage

parsers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import json
2+
import yaml
3+
4+
5+
class ParseException(Exception):
6+
pass
7+
8+
9+
def json_parser(file_buff):
10+
try:
11+
return json.loads(file_buff)
12+
except json.JSONDecodeError as e:
13+
raise ParseException(f'Unable to decode config file using json', e)
14+
15+
16+
def yaml_parser(file_buff):
17+
try:
18+
return yaml.safe_load(file_buff)
19+
except yaml.YAMLError as e:
20+
raise ParseException(f'Unable to decode config file using yaml', e)

pyconfigparser.py

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
1+
from parsers import ParseException, json_parser, yaml_parser
12
from schema import Schema, SchemaError
23
from typing import Any
34
from os import path
4-
import json
5-
import yaml
65
import os
76
import re
87

9-
10-
def json_parser(file_buff):
11-
try:
12-
return json.loads(file_buff)
13-
except json.JSONDecodeError as e:
14-
raise ConfigFileDecodeError(f'Unable to decode config file using json', e)
15-
16-
17-
def yaml_parser(file_buff):
18-
try:
19-
return yaml.safe_load(file_buff)
20-
except yaml.YAMLError as e:
21-
raise ConfigFileDecodeError(f'Unable to decode config file using yaml', e)
22-
23-
248
LINUX_KEY_VARIABLE_PATTERN = r'\$([a-zA-Z][\w]+|\{[a-zA-Z][\w]+\})$'
259
DEFAULT_CONFIG_FILES = ('config.json', 'config.yaml', 'config.yml')
2610
ENTITY_NAME_PATTERN = r'^[a-zA-Z][\w]+$'
27-
2811
SUPPORTED_EXTENSIONS = {
2912
'json': json_parser,
3013
'yaml': yaml_parser,
3114
'yml': yaml_parser
3215
}
3316

3417

18+
class ConfigError(Exception):
19+
pass
20+
21+
22+
class ConfigFileNotFoundError(ConfigError):
23+
pass
24+
25+
3526
class ConfigValue:
3627

3728
def __getitem__(self, item):
@@ -40,6 +31,15 @@ def __getitem__(self, item):
4031
def __iter__(self):
4132
return self.__dict__.keys().__iter__()
4233

34+
def __len__(self):
35+
return len(self.__dict__)
36+
37+
def keys(self):
38+
return self.__dict__.keys()
39+
40+
def values(self):
41+
return self.__dict__.values()
42+
4343

4444
class Config:
4545
__instance = None
@@ -147,15 +147,3 @@ def extract_env_variable_key(cls, variable):
147147
if variable[0] == '{':
148148
return variable[1:-1]
149149
return variable
150-
151-
152-
class ConfigError(Exception):
153-
pass
154-
155-
156-
class ConfigFileDecodeError(ConfigError):
157-
pass
158-
159-
160-
class ConfigFileNotFoundError(ConfigError):
161-
pass

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='python-config-parser',
8-
version='2.1.1',
8+
version='2.2.0',
99
author='Bruno Silva de Andrade',
1010
author_email='brunojf.andrade@gmail.com',
1111
description='Project created to given the possibility of create dynamics config files',
@@ -20,4 +20,4 @@
2020
'License :: OSI Approved :: MIT License',
2121
'Operating System :: OS Independent',
2222
]
23-
)
23+
)

test_configparser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os
2-
import unittest
1+
from pyconfigparser import Config, ConfigError, ConfigFileNotFoundError, ParseException
32
from config.schemas import SIMPLE_SCHEMA_CONFIG, UNSUPPORTED_OBJECT_KEYS_SCHEMA
4-
from pyconfigparser import Config, ConfigError, ConfigFileNotFoundError, ConfigFileDecodeError
3+
import unittest
4+
import os
55

66
DT_FMT_TEST = '%Y-%m-%dT%H:%M:%SZ'
77
VAR_LOG_LEVEL_INFO = 'INFO'
@@ -56,8 +56,8 @@ def test_config_file_with_unsupported_extension(self):
5656
self.assertRaises(ConfigError, Config.get_config, SIMPLE_SCHEMA_CONFIG, file_name='config.bad_extension')
5757

5858
def test_bad_decoder_error(self):
59-
self.assertRaises(ConfigFileDecodeError, Config.get_config, SIMPLE_SCHEMA_CONFIG, file_name='bad_content.json')
60-
self.assertRaises(ConfigFileDecodeError, Config.get_config, SIMPLE_SCHEMA_CONFIG, file_name='bad_content.yaml')
59+
self.assertRaises(ParseException, Config.get_config, SIMPLE_SCHEMA_CONFIG, file_name='bad_content.json')
60+
self.assertRaises(ParseException, Config.get_config, SIMPLE_SCHEMA_CONFIG, file_name='bad_content.yaml')
6161

6262

6363
if __name__ == '__main__':

0 commit comments

Comments
 (0)