mirror of
https://github.com/jlengrand/Observer.git
synced 2026-03-10 08:31:23 +00:00
Implements is_json_valid method
Checks whether the received json object is correct, and formatted as expected. Also tries to create a sublime-project. Let's see what it gives
This commit is contained in:
29
Observer.py
29
Observer.py
@@ -23,6 +23,8 @@ It is designed to use nothing but built-in python modules.
|
||||
Simply copy/paste this file and import Observer to start using it.
|
||||
"""
|
||||
|
||||
import json
|
||||
from StringIO import StringIO
|
||||
|
||||
class Observer():
|
||||
"""
|
||||
@@ -39,6 +41,7 @@ class Observer():
|
||||
raise TypeError("Expected string for name")
|
||||
|
||||
self.name = name
|
||||
self.group = None
|
||||
self.message = None
|
||||
|
||||
def update(self, message):
|
||||
@@ -112,8 +115,30 @@ class Observable():
|
||||
Shall be used for automatic polling.
|
||||
Sets current Observable value and notifies update to subscribers.
|
||||
"""
|
||||
if not isinstance(val, str):
|
||||
raise TypeError("Expected string for message")
|
||||
#if not isinstance(val, str):
|
||||
# raise TypeError("Expected string for message")
|
||||
|
||||
self.is_json_valid(val)
|
||||
|
||||
self.message = val
|
||||
self.__notify(self.message)
|
||||
|
||||
# JSON Stuff
|
||||
def is_json_valid(self, val):
|
||||
"""
|
||||
Returns True if val is a valid JSon Object.
|
||||
Valid means :
|
||||
- correctly formatted
|
||||
- contains ONLY all needed 4 sections.
|
||||
"""
|
||||
# json is valid
|
||||
mess = json.loads(val)
|
||||
|
||||
# Cheks for the presence of tags
|
||||
try:
|
||||
#simply access all
|
||||
expected = sorted(["name", "group", "type", "data"]) # should be placed somewhere else
|
||||
got = sorted(mess.keys())
|
||||
return (expected == got)
|
||||
except KeyError:
|
||||
return False
|
||||
Reference in New Issue
Block a user