-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the ability to create html from IO
- Loading branch information
Showing
20 changed files
with
387 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
Oops, something went wrong.