Skip to content

Commit

Permalink
+ Version 1.3.0
Browse files Browse the repository at this point in the history
+ Added support for empty objects
  • Loading branch information
Genarito committed Jul 27, 2021
1 parent cdd58a9 commit 6a5e5b0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 13 deletions.
35 changes: 24 additions & 11 deletions gura/GuraParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ def primitive_type(self):
:return: The corresponding matched value
"""
self.maybe_match('ws')
return self.match('null', 'boolean', 'basic_string', 'literal_string', 'number', 'variable_value')
return self.match('null', 'boolean', 'basic_string', 'literal_string', 'number', 'variable_value',
'empty_object')

def complex_type(self) -> Optional[Tuple[List, Dict]]:
"""
Expand Down Expand Up @@ -516,12 +517,20 @@ def __get_last_indentation_level(self) -> Optional[int]:

def null(self) -> MatchResult:
"""
Consumes null keyword and return None
Consumes 'null' keyword and returns None
:return None
"""
self.keyword('null')
return MatchResult(MatchResultType.PRIMITIVE, None)

def empty_object(self) -> MatchResult:
"""
Consumes 'empty' keyword and returns an empty object
:return Empty dict (which represents an object)
"""
self.keyword('empty')
return MatchResult(MatchResultType.PRIMITIVE, {})

def boolean(self) -> MatchResult:
"""
Parses boolean values
Expand Down Expand Up @@ -692,17 +701,21 @@ def dumps(self, value, indentation_level: int, new_line: bool) -> str:
if value_type == bool:
return ('true' if value is True else 'false') + new_line_char
if value_type == dict:
result = ''
indentation = ' ' * (indentation_level * 4)
for key, dict_value in value.items():
result += f'{indentation}{key}:'
# If it is an object it does not add a whitespace after key
if type(dict_value) != dict:
result += ' '
if len(value) > 0:
result = ''
indentation = ' ' * (indentation_level * 4)
for key, dict_value in value.items():
result += f'{indentation}{key}:'
# If it is an object it does not add a whitespace after key
if type(dict_value) != dict:
result += ' '

result += self.dumps(dict_value, indentation_level + 1, new_line=True)
return '\n' + result

result += self.dumps(dict_value, indentation_level + 1, new_line=True)
# Empty object
return ' empty\n'

return '\n' + result
if value_type == list:
# Lists are a special case: if it has an object, and indented representation must be returned. In case
# of primitive values or nested arrays, a plain representation is more appropriated
Expand Down
2 changes: 1 addition & 1 deletion gura/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
VariableNotDefinedError, DuplicatedImportError, loads, dumps
from gura.Parser import ParseError

__version__ = "1.2.0"
__version__ = "1.3.0"

loads = loads
dumps = dumps
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
author='JWare',
author_email='[email protected]',
url='https://github.com/gura-conf/gura-python-parser',
download_url='https://github.com/gura-conf/gura-python-parser/archive/refs/tags/1.2.0.tar.gz',
download_url='https://github.com/gura-conf/gura-python-parser/archive/refs/tags/1.3.0.tar.gz',
keywords=['Gura', 'parser', 'loads', 'dumps', 'encode', 'decode'],
install_requires=[
'wheel'
Expand Down
2 changes: 2 additions & 0 deletions tests/full/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def setUp(self):
"sf2": math.inf,
"sf3": -math.inf,
"null": None,
"empty_single": {},
"bool1": True,
"bool2": False,
"1234": "1234",
Expand Down Expand Up @@ -81,6 +82,7 @@ def setUp(self):
],
"my_server": {
"host": "127.0.0.1",
"empty_nested": {},
"port": 8080,
"native_auth": True
},
Expand Down
4 changes: 4 additions & 0 deletions tests/full/tests-files/full.ura
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ sf3: -inf # Negative infinity
# Null
null: null

# Empty objects
empty_single: empty

# Bool
bool1: true
bool2: false
Expand Down Expand Up @@ -91,6 +94,7 @@ $my_integer_var: 8080

my_server:
host: $my_string_var
empty_nested: empty
port: $my_integer_var
native_auth: true

Expand Down
20 changes: 20 additions & 0 deletions tests/objects/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class TestObjectsGura(unittest.TestCase):
file_dir: str
expected: Dict
empty_object: Dict

def setUp(self):
self.file_dir = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -30,6 +31,10 @@ def setUp(self):
}
}

self.empty_object = {
'empty_object': {}
}

def __get_file_parsed_data(self, file_name) -> Dict:
"""
Gets the content of a specific file parsed
Expand All @@ -46,6 +51,21 @@ def test_normal(self):
parsed_data = self.__get_file_parsed_data('normal.ura')
self.assertDictEqual(parsed_data, self.expected)

def test_empty(self):
"""Tests empty object"""
parsed_data = gura.loads('empty_object: empty')
self.assertDictEqual(parsed_data, self.empty_object)

def test_empty_2(self):
"""Tests empty object with several blanks"""
parsed_data = gura.loads('empty_object: empty ')
self.assertDictEqual(parsed_data, self.empty_object)

def test_empty_3(self):
"""Tests empty object with comments and blank lines"""
parsed_data = self.__get_file_parsed_data('empty.ura')
self.assertDictEqual(parsed_data, self.empty_object)

def test_with_comments(self):
"""Tests all kind of objects with comments between elements"""
parsed_data = self.__get_file_parsed_data('with_comments.ura')
Expand Down
6 changes: 6 additions & 0 deletions tests/objects/tests-files/empty.ura
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# COMMMENT
empty_object: empty



# COMMENT

0 comments on commit 6a5e5b0

Please sign in to comment.