Skip to content

Commit

Permalink
Done for now
Browse files Browse the repository at this point in the history
  • Loading branch information
washad committed Jun 23, 2019
1 parent a4c8500 commit 68fc337
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 64 deletions.
31 changes: 17 additions & 14 deletions build/lib/pyrediseasyio/html/html_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,39 @@

class HTMLIO:

def __init__(self, io: SingleIO, namespace: str='easyio'):
def __init__(self, io: SingleIO):
key = io.key
self.io = io
self.namespace = namespace
self.html_class = f'{self.namespace}_io'
self.container_id = f'{key}_io'
self.value_id = f'{key}_io_value'
self.name = io.name
self.val = io.value
self.addr = io.addr
self.value = io.value
self.units = io.units
self.html_id = f'{self.addr}_io_value'



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=addr) as container:
div(name, cls=f'{self.html_class}_name')
div(val, cls=f'{self.html_class}_value', id=self.html_id, onchange='OnIOValueChange(event)')
with div(cls='easyio_io', id=self.container_id) as container:
div(name, cls='easyio_name')
div(val, cls=f'easyio_value', id=self.value_id, onchange='OnIOValueChange(event)')
if show_units:
units = '' if units is None else units
div(units, cls=f'{self.html_class}_units')
div(units, cls=f'easyio_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=addr) as container:
td(name, cls=f'{self.html_class}_name')
td(val, cls=f'{self.html_class}_value', id=self.html_id, onchange='OnIOValueChange(event)')
with tr(cls='easyio_io', id=self.container_id) as container:
td(name, cls='easyio_name')
td(val, cls=f'easyio_value', id=self.value_id, onchange='OnIOValueChange(event)')
if show_units:
units = '' if units is None else units
td(units, cls=f'{self.html_class}_units')
td(units, cls=f'easyio_units')
return container


Expand Down
27 changes: 19 additions & 8 deletions build/lib/pyrediseasyio/html/html_io_group.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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
from dominate.tags import div, table, tr, th
import json


Expand All @@ -12,24 +12,29 @@ def __init__(self, io_group: IOGroup, html_id: str = None):
self.namespace = io_group.namespace
self.html_id = html_id



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):
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'
cls = f'easyio_io_container'
classes = html_classes.append(cls) if html_classes else [cls]
classes = ' '.join(classes)
html_id = f'{attrs[0].addr}_io_container' if self.html_id is None else self.html_id
html_id = f'{attrs[0].key}_io_container' if self.html_id is None else self.html_id

with div(cls=classes, id=html_id) as container:
for attr in attrs:
HTMLIO(attr, self.namespace).html()
HTMLIO(attr).html(show_units)
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):
html_classes: List = None, show_units: bool = True,
headers: List = None):

attrs = self._io_group.get_attributes(by_names, by_type, by_lambda_each, by_lambda_results)
cls = f'{self.namespace}_io_container'
Expand All @@ -38,18 +43,24 @@ def html_table(self, by_names: List = None, by_type: List = None,
html_id = f'{attrs[0].addr}_io_container' if self.html_id is None else self.html_id

with table(cls=classes, id=html_id) as container:
if headers:
with tr():
for h in headers:
th(h)
for attr in attrs:
HTMLIO(attr, self.namespace).html_row(show_units)
HTMLIO(attr).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):
io = HTMLIO(a)
return dict(id=io.html_id, name=io.name, value=io.val, units=io.units)
return dict(id=io.value_id, name=io.name, value=io.value, units=io.units)
results = [f(a) for a in attrs]

return json.dumps(results)
Expand Down
7 changes: 4 additions & 3 deletions build/lib/pyrediseasyio/io/io_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ def __init__(self, host='localhost', port=6379, db=0,
continue
attr._reader_writer = self
self.members.append(name)
new_name = name if attr.addr is None else attr.addr
attr.addr = new_name if namespace is None else f"{namespace}_{new_name}"

if attr.addr is None:
attr.addr = name
if namespace is not None:
attr.namespace = namespace
if delete_keys_on_startup:
self.delete_key(attr.addr)
if set_defaults_on_startup:
Expand Down
160 changes: 160 additions & 0 deletions build/lib/pyrediseasyio/io/single_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
from pyrediseasyio.abstract_reader_writer import AbstractReaderWriter
import threading
import json

lock = threading.Lock()


class SingleIO:
def __init__(self, name: str, addr: str = None, default: object = None,
units: str = None, reader: AbstractReaderWriter = None, namespace: str=None):
self.name = name
self.addr = addr
self.namespace = namespace
self._reader_writer = reader
self.default = default
self.units = units

def __and__(self, other):
if hasattr(other, 'value'):
return self.value and other.value
return self.value and other

def __add__(self, other):
if hasattr(other, 'value'):
other = other.value
return self.value + other

def __sub__(self, other):
if hasattr(other, 'value'):
other = other.value
return self.value - other

def __mul__(self, other):
if hasattr(other, 'value'):
other = other.value
return self.value * other

def __eq__(self, other):
if hasattr(other, 'value'):
other = other.value
return self.value == other

def __floordiv__(self, other):
if hasattr(other, 'value'):
other = other.value
return self.value // other

def __iadd__(self, other):
if hasattr(other, 'value'):
other = other.value
val = self.value
val += other
self.write(val)

def __idiv__(self, other):
if hasattr(other, 'value'):
other = other.value
val = self.value
val /= other
self.write(val)

def __ifloordiv__(self, other):
if hasattr(other, 'value'):
other = other.value
val = self.value
val //= other
self.write(val)

def __imul__(self, other):
if hasattr(other, 'value'):
other = other.value
val = self.value
val *= other
self.write(val)

def __invert__(self):
return ~self.value

def __ipow__(self, other):
if hasattr(other, 'value'):
other = other.value
val = self.value
val **= other
self.write(val)

def __isub__(self, other):
if hasattr(other, 'value'):
other = other.value
val = self.value
val -= other
self.write(val)

def __ne__(self, other):
if hasattr(other, 'value'):
other = other.value
return self.value != other

def __or__(self, other):
if hasattr(other, 'value'):
other = other.value
return self.value or other

def __pos__(self):
return self.value

def __pow__(self, power, modulo=None):
if hasattr(power, 'value'):
power = power.value
return self.value ** power

def __neg__(self):
return -self.value

def __set__(self, obj, value):
self.write(value)

def __str__(self):
return f'[{type(self).__name__}] {self.name} = {self.value} {self.units}'

def __truediv__(self, other):
if hasattr(other, 'value'):
other = other.value
return self.value / other

@property
def key(self):
return f'{self.namespace}{self.addr}'

@property
def value(self):
return self.read()

@staticmethod
def _convert_type(value):
return value

def publish(self, value, channel: str = None, and_write: bool = False):
value = self._convert_type(value)
data = json.dumps({self.key: value})
self._reader_writer.publish(data, channel)
if and_write:
self.write(value)

def read(self):
if self._reader_writer is None:
return None
with lock:
val = self._reader_writer.read(self.key)
if val is None:
val = self.default
val = self._convert_type(val)
return val

def write(self, value):
if self._reader_writer is None:
return
with lock:
value = self._convert_type(value)
self._reader_writer.write(self.key, value)

6 changes: 3 additions & 3 deletions build/lib/tests/test_get_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestGroup2(IOGroup):


test_group = TestGroup()
test_group2 = TestGroup2(namespace="testns")
test_group2 = TestGroup2(namespace="Test")


class TestAttributes(unittest.TestCase):
Expand All @@ -50,6 +50,6 @@ def test_limit_by_io_result(self):
assert_that(attrs[0].value).is_equal_to(34)

def test_namespace(self):
assert_that(test_group2.Bool1.addr).is_equal_to(f'testns_Bool1')
assert_that(test_group2.Float1.addr).is_equal_to('testns_MyName')
assert_that(test_group2.Bool1.key).is_equal_to(f'TestBool1')
assert_that(test_group2.Float1.key).is_equal_to('TestMyName')

Loading

0 comments on commit 68fc337

Please sign in to comment.