Skip to content

Commit

Permalink
Added the ability to create html from IO
Browse files Browse the repository at this point in the history
  • Loading branch information
washad committed Jun 22, 2019
1 parent efaa19a commit 075a7c3
Show file tree
Hide file tree
Showing 20 changed files with 387 additions and 61 deletions.
2 changes: 1 addition & 1 deletion build/lib/pyrediseasyio/io_group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pyrediseasyio.reader_writer import ReaderWriter
from pyrediseasyio.single_io import SingleIO
from pyrediseasyio.io.base import SingleIO
import json


Expand Down
4 changes: 2 additions & 2 deletions build/lib/tests/test_read_write.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from pyrediseasyio.io_group import IOGroup
from pyrediseasyio.single_io import BooleanIO, IntIO, FloatIO, StringIO
from pyrediseasyio.io.io_group import IOGroup
from pyrediseasyio.io.base import BooleanIO, IntIO, FloatIO, StringIO
from assertpy import assert_that


Expand Down
Binary file removed dist/pyrediseasyio-0.0.11-py3-none-any.whl
Binary file not shown.
Binary file removed dist/pyrediseasyio-0.0.11.tar.gz
Binary file not shown.
8 changes: 8 additions & 0 deletions pyrediseasyio/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pyrediseasyio.io.io_group import IOGroup
from pyrediseasyio.html.html_io_group import HMTLIOGroup
from pyrediseasyio.html.html_io import HTMLIO
from pyrediseasyio.io.base import SingleIO
from pyrediseasyio.io.boolean_io import BooleanIO
from pyrediseasyio.io.string_io import StringIO
from pyrediseasyio.io.float_io import FloatIO
from pyrediseasyio.io.integer_io import IntIO
Empty file added pyrediseasyio/html/__init__.py
Empty file.
52 changes: 52 additions & 0 deletions pyrediseasyio/html/html_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from pyrediseasyio.io.base import SingleIO
from dominate.tags import div, tr, td



class HTMLIO:

def __init__(self, io: SingleIO, html_id_header: str, namespace: str = 'pyredeio'):
self.io = io
self.namespace = namespace
self.html_id_header = html_id_header

@staticmethod
def html_id_for(header: str, attr: SingleIO):
return f'{header}_{attr.addr}_io'

@property
def html_id(self):
return self.html_id_for(self.html_id_header, self.io)

@property
def html_class(self):
return f'{self.namespace}_io'

def html(self, show_units: bool = True):
io = self.io
name, addr, val, units = io.name, io.addr, io.value, io.units
with div(cls=self.html_class, id=self.html_id) as container:
div(name, cls=f'{self.html_class}_name')
div(val, cls=f'{self.html_class}_value', id=f'{self.html_id}_value', onchange='OnIOValueChange(event)')
if show_units:
units = '' if units is None else units
div(units, cls=f'{self.html_class}_units')
return container

def html_row(self, show_units: bool = True):
io = self.io
name, addr, val, units = io.name, io.addr, io.value, io.units
with tr(cls=self.html_class, id=self.html_id) as container:
td(name, cls=f'{self.html_class}_name')
td(val, cls=f'{self.html_class}_value', id=f'{self.html_id}_value', onchange='OnIOValueChange(event)')
if show_units:
units = '' if units is None else units
td(units, cls=f'{self.html_class}_units')
return container







61 changes: 61 additions & 0 deletions pyrediseasyio/html/html_io_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from pyrediseasyio.io.io_group import IOGroup
from pyrediseasyio.html.html_io import HTMLIO
from typing import List, Callable
from dominate.tags import div, table
import json


class HMTLIOGroup:

def __init__(self, io_group: IOGroup,
html_id_header: str,
namespace: str = 'pyredeio'):

self.html_id_header = html_id_header
self._io_group = io_group
self.namespace = namespace

@property
def html_id(self):
return f'{self.html_id_header}_io_container'

def html(self, by_names: List = None, by_type: List = None,
by_lambda_each: Callable = None, by_lambda_results: Callable = None,
html_classes: List = None):

attrs = self._io_group.get_attributes(by_names, by_type, by_lambda_each, by_lambda_results)
cls = f'{self.namespace}_io_container'
classes = html_classes.append(cls) if html_classes else [cls]
classes = ' '.join(classes)

with div(cls=classes, id=self.html_id) as container:
for attr in attrs:
HTMLIO(attr, self.html_id_header, self.namespace).html()
return container

def html_table(self, by_names: List = None, by_type: List = None,
by_lambda_each: Callable = None, by_lambda_results: Callable = None,
html_classes: List = None, show_units: bool=True):

attrs = self._io_group.get_attributes(by_names, by_type, by_lambda_each, by_lambda_results)
cls = f'{self.namespace}_io_container'
classes = html_classes.append(cls) if html_classes else [cls]
classes = ' '.join(classes)

with table(cls=classes, id=self.html_id) as container:
for attr in attrs:
HTMLIO(attr, self.html_id_header, self.namespace).html_row(show_units)
return container

def dumps(self, by_names: List = None, by_type: List = None,
by_lambda_each: Callable = None, by_lambda_results: Callable = None):

attrs = self._io_group.get_attributes(by_names, by_type, by_lambda_each, by_lambda_results)

def f(a):
return dict(id=HTMLIO.html_id_for(self.html_id_header, a), name=a.name, value=a.value, units=a.units)
results = [f(a) for a in attrs]

return json.dumps(results)


Empty file added pyrediseasyio/io/__init__.py
Empty file.
48 changes: 1 addition & 47 deletions pyrediseasyio/single_io.py → pyrediseasyio/io/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pyrediseasyio.abstract_reader_writer import AbstractReaderWriter
from dominate.tags import div, span, tr, td
import threading
from str2bool import str2bool
import json

lock = threading.Lock()
Expand Down Expand Up @@ -94,49 +94,3 @@ def write(self, value):
value = self._convert_type(value)
self._reader_writer.write(self.addr, value)


class BooleanIO(SingleIO):
def __init__(self, name: str, addr: str, default: bool = False, units: str = None, reader: AbstractReaderWriter = None):
super().__init__(name, addr, default, units, reader)

@staticmethod
def _convert_type(value):
if isinstance(value, str):
return str2bool(value)
return bool(value)

def __bool__(self):
return self.value

@property
def value(self) -> bool:
return super().value


class IntIO(SingleIO):
def __init__(self, name: str, addr: str, default: int = 0, units: str = None, reader: AbstractReaderWriter = None):
super().__init__(name, addr, default, units, reader)

@staticmethod
def _convert_type(value):
return int(value)


class FloatIO(SingleIO):
def __init__(self, name: str, addr: str, default: float = 0, units: str = None, reader: AbstractReaderWriter = None):
super().__init__(name, addr, default, units, reader)

@staticmethod
def _convert_type(value):
return float(value)


class StringIO(SingleIO):
def __init__(self, name: str, addr: str, default: str = '', units: str = None, reader: AbstractReaderWriter = None):
super().__init__(name, addr, default, units, reader)

@staticmethod
def _convert_type(value):
return str(value)


24 changes: 24 additions & 0 deletions pyrediseasyio/io/boolean_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pyrediseasyio import SingleIO
from pyrediseasyio.abstract_reader_writer import AbstractReaderWriter
from str2bool import str2bool


class BooleanIO(SingleIO):
def __init__(self, name: str, addr: str, default: bool = False, units: str = None,
reader: AbstractReaderWriter = None):
super().__init__(name, addr, default, units, reader)

@staticmethod
def _convert_type(value):
if value is None:
return False
if isinstance(value, str):
return str2bool(value)
return bool(value)

def __bool__(self):
return self.value

@property
def value(self) -> bool:
return super().value
13 changes: 13 additions & 0 deletions pyrediseasyio/io/float_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pyrediseasyio import SingleIO
from pyrediseasyio.abstract_reader_writer import AbstractReaderWriter


class FloatIO(SingleIO):
def __init__(self, name: str, addr: str, default: float = 0, units: str = None, reader: AbstractReaderWriter = None):
super().__init__(name, addr, default, units, reader)

@staticmethod
def _convert_type(value):
return float(value)


14 changes: 14 additions & 0 deletions pyrediseasyio/io/integer_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pyrediseasyio import SingleIO
from pyrediseasyio.abstract_reader_writer import AbstractReaderWriter


class IntIO(SingleIO):
def __init__(self, name: str, addr: str, default: int = 0, units: str = None, reader: AbstractReaderWriter = None):
super().__init__(name, addr, default, units, reader)

@staticmethod
def _convert_type(value):
return int(value)



32 changes: 26 additions & 6 deletions pyrediseasyio/io_group.py → pyrediseasyio/io/io_group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pyrediseasyio.reader_writer import ReaderWriter
from pyrediseasyio.single_io import SingleIO
from pyrediseasyio.io.base import SingleIO
from dominate.tags import div, span, table, tr
from typing import List, Callable
import json


Expand All @@ -26,6 +28,9 @@ def __init__(self, host='localhost', port=6379, db=0,
except AttributeError:
pass

def __len__(self):
return len(self.members)

def __setattr__(self, key, value):
if hasattr(self, 'members') and key in self.members:
attr = getattr(self, key)
Expand All @@ -44,16 +49,31 @@ def dump(self, key: str) -> str:
self.write(key, s)
return s

def dumps(self) -> str:
def dumps(self, by_names: List = None, by_type: List = None, by_lambda: Callable = None) -> str:
"""
Returns a json string containing a list of dict(name/addr/value) of all members.
"""
members = []
for m in self.members:
attr = getattr(self, m)
members.append(dict(name=attr.name, addr=attr.addr, value=attr.value))
attrs = self.get_attributes(by_names, by_type, by_lambda)
members = [dict(name=attr.name, addr=attr.addr, value=attr.value) for attr in attrs]
return json.dumps(members)

def get_attributes(self, by_names: List = None, by_type: List = None,
by_lambda_each: Callable = None, by_lambda_results: Callable = None):

names = self.members if by_names is None else by_names
attrs = [getattr(self, name) for name in names]
if by_type:
attrs = [a for a in attrs if type(a) in by_type]
if by_lambda_each:
attrs = [a for a in attrs if by_lambda_each(a)]
if by_lambda_results:
attrs = by_lambda_results(attrs)

return attrs







11 changes: 11 additions & 0 deletions pyrediseasyio/io/string_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pyrediseasyio.abstract_reader_writer import AbstractReaderWriter
from pyrediseasyio import SingleIO


class StringIO(SingleIO):
def __init__(self, name: str, addr: str, default: str = '', units: str = None, reader: AbstractReaderWriter = None):
super().__init__(name, addr, default, units, reader)

@staticmethod
def _convert_type(value):
return str(value)
41 changes: 41 additions & 0 deletions tests/test_get_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
from pyrediseasyio.io.io_group import IOGroup
from pyrediseasyio import BooleanIO, IntIO, FloatIO
from assertpy import assert_that


class TestGroup(IOGroup):
length = 5
Bool1 = BooleanIO("Boolean 1", "Bool1", False)
Bool2 = BooleanIO("Boolean 2", "Bool2", True)
Int1 = IntIO("Integer 1", "Int1")
Int2 = IntIO("Integer 2", "Int2", default=34)
Float1 = FloatIO("Float 1", "Float1", default=1.2)


test_group = TestGroup()


class TestAttributes(unittest.TestCase):

def test_get_all_attributes(self):
attrs = test_group.get_attributes()
assert_that(len(attrs)).is_equal_to(TestGroup.length)

def test_limit_attributes_by_names(self):
attrs = test_group.get_attributes(by_names=['Bool1','Float1'])
assert_that(len(attrs)).is_equal_to(2)

def test_limit_attributes_by_type(self):
attrs = test_group.get_attributes(by_type=[IntIO, FloatIO])
assert_that(len(attrs)).is_equal_to(3)

def test_limit_by_io_filter(self):
attrs = test_group.get_attributes(by_lambda_each=lambda x: x.value == 1.2)
assert_that(len(attrs)).is_equal_to(1)

def test_limit_by_io_result(self):
attrs = test_group.get_attributes(by_lambda_results=lambda x: x[-1:])
assert_that(len(attrs)).is_equal_to(1)
assert_that(attrs[0].value).is_equal_to(34)

Loading

0 comments on commit 075a7c3

Please sign in to comment.