Add a new method write_json

merge-requests/64/head
Hugo Posnic 2017-05-21 20:38:21 +02:00
parent 9ef552310f
commit 0d235362a8
1 changed files with 15 additions and 5 deletions

View File

@ -211,11 +211,7 @@ class ManjaroHello():
def save_preferences(self): def save_preferences(self):
"""Save preferences in config file.""" """Save preferences in config file."""
try: write_json(self.preferences_path, self.preferences)
with open(self.preferences_path, "w") as f:
json.dump(self.preferences, f)
except OSError as error:
print(error)
def get_preferences(self): def get_preferences(self):
"""Read preferences from config file.""" """Read preferences from config file."""
@ -275,6 +271,8 @@ class ManjaroHello():
def read_json(path): def read_json(path):
"""Read content of a json file. """Read content of a json file.
:param path: path of file to read
:type path: str
:return: json content :return: json content
:rtype: str :rtype: str
""" """
@ -284,6 +282,18 @@ def read_json(path):
except OSError: except OSError:
return None return None
def write_json(path, content):
"""Write content in a json file.
:param path: path of file to write
:type path: str
:param content: content to write
:type path: str
"""
try:
with open(path, "w") as f:
json.dump(content, f)
except OSError as error:
print(error)
if __name__ == "__main__": if __name__ == "__main__":
ManjaroHello() ManjaroHello()