mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-07-22 11:59:33 +00:00
utils: Fix ruamel.yaml deprecation warnings
Fixes #2143 Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net> [sunil: Convert the class property to instance property for added safety] Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
parent
d57dd8301c
commit
af53064660
@ -8,6 +8,7 @@ from unittest.mock import MagicMock, Mock
|
||||
|
||||
import pytest
|
||||
import ruamel.yaml
|
||||
from ruamel.yaml.compat import StringIO
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from plinth.utils import YAMLFile, is_user_admin, is_valid_user_name
|
||||
@ -90,6 +91,9 @@ class TestYAMLFileUtil:
|
||||
|
||||
kv_pair = {'key': 'value'}
|
||||
|
||||
yaml = ruamel.yaml.YAML()
|
||||
yaml.preserve_quotes = True
|
||||
|
||||
def test_update_empty_yaml_file(self):
|
||||
"""
|
||||
Update an empty YAML file with content.
|
||||
@ -102,7 +106,9 @@ class TestYAMLFileUtil:
|
||||
file_conf[key] = conf[key]
|
||||
|
||||
with open(test_file.name, 'r') as retrieved_conf:
|
||||
assert retrieved_conf.read() == ruamel.yaml.round_trip_dump(conf)
|
||||
buffer = StringIO()
|
||||
self.yaml.dump(conf, buffer)
|
||||
assert retrieved_conf.read() == buffer.getvalue()
|
||||
|
||||
def test_update_non_empty_yaml_file(self):
|
||||
"""
|
||||
@ -111,14 +117,13 @@ class TestYAMLFileUtil:
|
||||
test_file = tempfile.NamedTemporaryFile()
|
||||
|
||||
with open(test_file.name, 'w') as conf_file:
|
||||
conf_file.write(
|
||||
ruamel.yaml.round_trip_dump({'property1': self.kv_pair}))
|
||||
self.yaml.dump({'property1': self.kv_pair}, conf_file)
|
||||
|
||||
with YAMLFile(test_file.name) as file_conf:
|
||||
file_conf['property2'] = self.kv_pair
|
||||
|
||||
with open(test_file.name, 'r') as retrieved_conf:
|
||||
file_conf = ruamel.yaml.round_trip_load(retrieved_conf)
|
||||
file_conf = self.yaml.load(retrieved_conf)
|
||||
assert file_conf == {
|
||||
'property1': self.kv_pair,
|
||||
'property2': self.kv_pair
|
||||
|
||||
@ -102,10 +102,13 @@ class YAMLFile(object):
|
||||
self.yaml_file = yaml_file
|
||||
self.conf = None
|
||||
|
||||
self.yaml = ruamel.yaml.YAML()
|
||||
self.yaml.preserve_quotes = True
|
||||
|
||||
def __enter__(self):
|
||||
with open(self.yaml_file, 'r') as intro_conf:
|
||||
if not self.is_file_empty():
|
||||
self.conf = ruamel.yaml.round_trip_load(intro_conf)
|
||||
self.conf = self.yaml.load(intro_conf)
|
||||
else:
|
||||
self.conf = {}
|
||||
|
||||
@ -114,7 +117,7 @@ class YAMLFile(object):
|
||||
def __exit__(self, type_, value, traceback):
|
||||
if not traceback:
|
||||
with open(self.yaml_file, 'w') as intro_conf:
|
||||
ruamel.yaml.round_trip_dump(self.conf, intro_conf)
|
||||
self.yaml.dump(self.conf, intro_conf)
|
||||
|
||||
def is_file_empty(self):
|
||||
return os.stat(self.yaml_file).st_size == 0
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user