diff --git a/Pipfile b/Pipfile index 4c2d247..1fb503b 100644 --- a/Pipfile +++ b/Pipfile @@ -13,6 +13,7 @@ colorama = "*" click-log = "*" click-didyoumean = "*" voluptuous = "*" +terminaltables = "*" [dev-packages] "flake8" = "*" diff --git a/Pipfile.lock b/Pipfile.lock index 981f185..c38964c 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "07aac5a19e64863bc015b63d301912dc79586e7be06dd8056b60dd0e6fb26629" + "sha256": "471710975bfaa4492e9efc5df510666744e783264972376f1c14860496a536b1" }, "pipfile-spec": 6, "requires": { @@ -40,6 +40,7 @@ }, "click-didyoumean": { "hashes": [ + "sha256:0b427ca37c9b85d92e75a915db95c1ac0d6efd65e8b89139eed398bbda074bd8", "sha256:112229485c9704ff51362fe34b2d4f0b12fc71cc20f6d2b3afabed4b8bfa6aeb" ], "index": "pypi", @@ -55,11 +56,11 @@ }, "colorama": { "hashes": [ - "sha256:a3d89af5db9e9806a779a50296b5fdb466e281147c2c235e8225ecc6dbf7bbf3", - "sha256:c9b54bebe91a6a803e0772c8561d53f2926bfeb17cd141fbabcb08424086595c" + "sha256:05eed71e2e327246ad6b38c540c4a3117230b19679b875190486ddd2d721422d", + "sha256:f8ac84de7840f5b9c4e3347b3c1eaa50f7e49c2b07596221daec5edaabbd7c48" ], "index": "pypi", - "version": "==0.4.0" + "version": "==0.4.1" }, "gpiozero": { "hashes": [ @@ -94,12 +95,19 @@ }, "rpi.gpio": { "hashes": [ - "sha256:3a08d6ddf2fef5f82a8c981fa2e988a296c43e9c6755e8df691a89ad7620115d", "sha256:a4210ad63bfe844e43995286de0d3950dfacfa0f3799bb9392770ac54a7d2e47" ], "index": "pypi", "version": "==0.6.5" }, + "terminaltables": { + "hashes": [ + "sha256:e24fb83140a5b04fac4f17e1996cc74809188de2896fb3a98eb1b670cc52bbdd", + "sha256:f3eb0eb92e3833972ac36796293ca0906e998dc3be91fbe1f8615b331b853b81" + ], + "index": "pypi", + "version": "==3.1.0" + }, "urllib3": { "hashes": [ "sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39", @@ -161,11 +169,11 @@ }, "colorama": { "hashes": [ - "sha256:a3d89af5db9e9806a779a50296b5fdb466e281147c2c235e8225ecc6dbf7bbf3", - "sha256:c9b54bebe91a6a803e0772c8561d53f2926bfeb17cd141fbabcb08424086595c" + "sha256:05eed71e2e327246ad6b38c540c4a3117230b19679b875190486ddd2d721422d", + "sha256:f8ac84de7840f5b9c4e3347b3c1eaa50f7e49c2b07596221daec5edaabbd7c48" ], "index": "pypi", - "version": "==0.4.0" + "version": "==0.4.1" }, "docutils": { "hashes": [ @@ -227,7 +235,6 @@ }, "markupsafe": { "hashes": [ - "sha256:5e0432a10c901c46e4de52574d1d396bdde0bec8715496961546099c4e0ff16d", "sha256:048ef924c1623740e70204aa7143ec592504045ae4429b59c30054cb31e3c432", "sha256:130f844e7f5bdd8e9f3f42e7102ef1d49b2e6fdf0d7526df3f87281a532d8c8b", "sha256:19f637c2ac5ae9da8bfd98cef74d64b7e1bb8a63038a3505cd182c3fac5eb4d9", @@ -314,11 +321,11 @@ }, "pipenv": { "hashes": [ - "sha256:117b0015837578cef25ee09c9d38094245966cbff92f0984e0f0fbebdbb5bc75", - "sha256:5f760d35de14982353437442d0d2c474bee139ffd69c01148abc11509f6422da", - "sha256:7e4b827307e6070468284caa332fcba9c60e532afd2a8ff4c0d6df8be6b9fadb" + "sha256:56ad5f5cb48f1e58878e14525a6e3129d4306049cb76d2f6a3e95df0d5fc6330", + "sha256:7df8e33a2387de6f537836f48ac6fcd94eda6ed9ba3d5e3fd52e35b5bc7ff49e", + "sha256:a673e606e8452185e9817a987572b55360f4d28b50831ef3b42ac3cab3fee846" ], - "version": "==2018.11.14" + "version": "==2018.11.26" }, "pluggy": { "hashes": [ @@ -350,10 +357,10 @@ }, "pygments": { "hashes": [ - "sha256:78f3f434bcc5d6ee09020f92ba487f95ba50f1e3ef83ae96b9d5ffa1bab25c5d", - "sha256:dbae1046def0efb574852fab9e90209b23f556367b5a320c0bcb871c77c3e8cc" + "sha256:6301ecb0997a52d2d31385e62d0a4a4cf18d2f2da7054a5ddad5c366cd39cee7", + "sha256:82666aac15622bd7bb685a4ee7f6625dd716da3ef7473620c192c0168aae64fc" ], - "version": "==2.2.0" + "version": "==2.3.0" }, "pyparsing": { "hashes": [ @@ -364,11 +371,11 @@ }, "pytest": { "hashes": [ - "sha256:488c842647bbeb350029da10325cb40af0a9c7a2fdda45aeb1dda75b60048ffb", - "sha256:c055690dfefa744992f563e8c3a654089a6aa5b8092dded9b6fafbd70b2e45a7" + "sha256:1d131cc532be0023ef8ae265e2a779938d0619bb6c2510f52987ffcba7fa1ee4", + "sha256:ca4761407f1acc85ffd1609f464ca20bb71a767803505bd4127d0e45c5a50e23" ], "index": "pypi", - "version": "==4.0.0" + "version": "==4.0.1" }, "pytz": { "hashes": [ diff --git a/carom/cli.py b/carom/cli.py index 60df687..5d82c43 100644 --- a/carom/cli.py +++ b/carom/cli.py @@ -5,6 +5,10 @@ from click_didyoumean import DYMGroup import click_log import logging +from terminaltables import SingleTable + +from carom.table import cleanup + logger = logging.getLogger('carom.cli') # hard coded module name to support directly called module root_logger = logging.getLogger('') click_log.basic_config(root_logger) @@ -21,7 +25,14 @@ def get_gpio(): """ Get the current status of all Tables. """ - pass + from carom.table import tables + table_data = [("Table #", "Port", "in use")] + for table in tables: + table_data.append((table.num, table.port, table.in_use)) + st = SingleTable(table_data) + st.title = "Tables" + print(st.table) + @cli.command() @@ -55,4 +66,7 @@ def check_config(): if __name__ == "__main__": - cli() + try: + cli() + finally: + cleanup() diff --git a/carom/table.py b/carom/table.py new file mode 100644 index 0000000..a8b9eb1 --- /dev/null +++ b/carom/table.py @@ -0,0 +1,45 @@ +import logging + +from gpiozero import Button + +from carom.config import config +from carom.utils import singleton + +logger = logging.getLogger(__name__) + + +@singleton +class Table: + def __init__(self, num, port): + self.num = num + self.port = port + self.button = Button(port, pull_up=False) + + def __repr__(self): + return "".format(self.num, self.port, self.in_use) + + def __del__(self): + self.close() + + + @property + def in_use(self): + return self.button.is_active + + def close(self): + logger.debug("Closing GPIO {}".format(self.port)) + self.button.close() + + +def cleanup(): + for table in tables: + table.close() + +def _populate_tables(): + for num, port in config['mapping'].items(): + yield Table(num, port) + + +tables = [table for table in _populate_tables()] + +__all__ = ('Table', 'tables', 'cleanup') diff --git a/carom/utils.py b/carom/utils.py new file mode 100644 index 0000000..cfbe019 --- /dev/null +++ b/carom/utils.py @@ -0,0 +1,13 @@ +def singleton(the_class): + """ decorator for a class to make a singleton out of it """ + class_instances = {} + + def get_instance(*args, **kwargs): + """ creating or just return the one and only class instance. + The singleton depends on the parameters used in __init__ """ + key = (the_class, args, str(kwargs)) + if key not in class_instances: + class_instances[key] = the_class(*args, **kwargs) + return class_instances[key] + + return get_instance