Skip to content

Commit 842dc94

Browse files
committed
JSON serialization implemented
1 parent dd37ca2 commit 842dc94

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ To explore the WebUI demo execute the following testcase
7474
```sh
7575
$ MANUAL=1 python -m unittest mpetests.TestWebUI.test_failures
7676
```
77-
and open http://localhost:8080 in the browser.
77+
and open http://localhost:8081 (or :8080) in the browser.
7878

7979
## Installation
8080

mpepool.py

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

309+
310+
def json(self):
311+
"""Serialize self to the JSON representation"""
312+
return {p: self.__getattribute__(p) if p != 'task' else self.__getattribute__(p).name for p in self.iterprop()}
313+
314+
308315
def iterprop(cls):
309316
"""Properties generator/iterator"""
310317
ib = 0
@@ -327,6 +334,7 @@ def iterprop(cls):
327334
# ATTENTION: the methods are bound automatically to self (but not to the cls in Python2)
328335
# since they are defined before the class is created.
329336
cls.__contains__ = contains
337+
cls.json = json
330338
cls.iterprop = types.MethodType(iterprop, cls) # Note: required only in Python2 for the static methods
331339
return cls
332340

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

555563

564+
def json(self):
565+
"""Serialize self to the JSON representation"""
566+
return {p: self.__getattribute__(p) for p in self.__slots__}
567+
568+
556569
def unfoldDepthFirst(tinfext, indent=0):
557570
"""Print TaskInfoExt hierarchy using the depth first traversing
558571

mpewui.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,17 @@ def __init__(self, workers=0, jobs=0, jobsDone=0, jobsFailed=0, tasksFailed=0
346346
self.tasksRoot = tasksRoot
347347
self.tasksRootFailed = tasksRootFailed
348348

349+
349350
# @property
350351
# def __dict__(self):
351352
# return dict({p: self.__getattribute__(p) for p in self.__slots__})
352353

353354

355+
def json(self):
356+
"""Serialize self to the JSON representation"""
357+
return {p: self.__getattribute__(p) for p in self.__slots__}
358+
359+
354360
class WebUiApp(threading.Thread):
355361
"""WebUI App starting in the dedicated thread and providing remote interface to inspect ExecPool"""
356362
RAM = None
@@ -510,8 +516,7 @@ def root(cmd):
510516

511517
# Expected format of data is a table: header, rows
512518
if resopts.fmt == UiResFmt.json:
513-
# TODO: implements JSON serializer
514-
return json.dumps(cmd.data)
519+
return json.dumps(cmd.data, default=lambda obj: obj.json())
515520
elif resopts.fmt == UiResFmt.txt:
516521
# 501 - Not Implemented
517522
bottle.response.status = 501
@@ -582,8 +587,7 @@ def jobs(cmd):
582587
return cmderr
583588

584589
if resopts.fmt == UiResFmt.json:
585-
# TOFIX: implements JSON serializer
586-
return json.dumps(cmd.data)
590+
return json.dumps(cmd.data, default=lambda obj: obj.json())
587591
elif resopts.fmt == UiResFmt.txt:
588592
# 501 - Not Implemented
589593
bottle.response.status = 501
@@ -647,8 +651,7 @@ def tasks(cmd):
647651
return cmderr
648652

649653
if resopts.fmt == UiResFmt.json:
650-
# TOFIX: implements JSON serializer
651-
return json.dumps(cmd.data)
654+
return json.dumps(cmd.data, default=lambda obj: obj.json())
652655
elif resopts.fmt == UiResFmt.txt:
653656
# 501 - Not Implemented
654657
bottle.response.status = 501

0 commit comments

Comments
 (0)