Skip to content

Commit

Permalink
JSON serialization implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
luav committed Jun 21, 2018
1 parent dd37ca2 commit 842dc94
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ To explore the WebUI demo execute the following testcase
```sh
$ MANUAL=1 python -m unittest mpetests.TestWebUI.test_failures
```
and open http://localhost:8080 in the browser.
and open http://localhost:8081 (or :8080) in the browser.

## Installation

Expand Down
13 changes: 13 additions & 0 deletions mpepool.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def propslist(cls):
- _props: set - all public attributes of the class
and all __slots__ members even if the latter are underscored
- `in` operator support added to test membership in the _props
- json - dict representation for the JSON serialization
- iterprop() method to iterate over the properties present in _props starting
from __slots__ in the order of declaration and then over the computed properties
in the alphabetical order
Expand All @@ -305,6 +306,12 @@ def contains(self, prop):
assert len(self._props) >= 2, 'At least 2 properties are expected'
return self._props.endswith(' ' + prop) or self._props.find(prop + ' ') != -1 #pylint: disable=W0212


def json(self):
"""Serialize self to the JSON representation"""
return {p: self.__getattribute__(p) if p != 'task' else self.__getattribute__(p).name for p in self.iterprop()}


def iterprop(cls):
"""Properties generator/iterator"""
ib = 0
Expand All @@ -327,6 +334,7 @@ def iterprop(cls):
# ATTENTION: the methods are bound automatically to self (but not to the cls in Python2)
# since they are defined before the class is created.
cls.__contains__ = contains
cls.json = json
cls.iterprop = types.MethodType(iterprop, cls) # Note: required only in Python2 for the static methods
return cls

Expand Down Expand Up @@ -553,6 +561,11 @@ def __init__(self, data, ident=0, compound=None):
self.data = data


def json(self):
"""Serialize self to the JSON representation"""
return {p: self.__getattribute__(p) for p in self.__slots__}


def unfoldDepthFirst(tinfext, indent=0):
"""Print TaskInfoExt hierarchy using the depth first traversing
Expand Down
15 changes: 9 additions & 6 deletions mpewui.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,17 @@ def __init__(self, workers=0, jobs=0, jobsDone=0, jobsFailed=0, tasksFailed=0
self.tasksRoot = tasksRoot
self.tasksRootFailed = tasksRootFailed


# @property
# def __dict__(self):
# return dict({p: self.__getattribute__(p) for p in self.__slots__})


def json(self):
"""Serialize self to the JSON representation"""
return {p: self.__getattribute__(p) for p in self.__slots__}


class WebUiApp(threading.Thread):
"""WebUI App starting in the dedicated thread and providing remote interface to inspect ExecPool"""
RAM = None
Expand Down Expand Up @@ -510,8 +516,7 @@ def root(cmd):

# Expected format of data is a table: header, rows
if resopts.fmt == UiResFmt.json:
# TODO: implements JSON serializer
return json.dumps(cmd.data)
return json.dumps(cmd.data, default=lambda obj: obj.json())
elif resopts.fmt == UiResFmt.txt:
# 501 - Not Implemented
bottle.response.status = 501
Expand Down Expand Up @@ -582,8 +587,7 @@ def jobs(cmd):
return cmderr

if resopts.fmt == UiResFmt.json:
# TOFIX: implements JSON serializer
return json.dumps(cmd.data)
return json.dumps(cmd.data, default=lambda obj: obj.json())
elif resopts.fmt == UiResFmt.txt:
# 501 - Not Implemented
bottle.response.status = 501
Expand Down Expand Up @@ -647,8 +651,7 @@ def tasks(cmd):
return cmderr

if resopts.fmt == UiResFmt.json:
# TOFIX: implements JSON serializer
return json.dumps(cmd.data)
return json.dumps(cmd.data, default=lambda obj: obj.json())
elif resopts.fmt == UiResFmt.txt:
# 501 - Not Implemented
bottle.response.status = 501
Expand Down

0 comments on commit 842dc94

Please sign in to comment.