From 62bdd68c5a3294ccfbc4a543547a2343308eea7c Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Sun, 7 Mar 2021 16:24:04 +0100 Subject: [PATCH] change singleton to global --- InputHandler.py | 5 ++--- Solenoid.py | 4 +++- __main__.py | 9 ++++++--- displays/Display.py | 5 ++--- factories/GameFactory.py | 5 ++--- factories/NetworkFactory.py | 2 +- lamps/Lamp.py | 8 ++++---- lamps/LampGroup.py | 2 ++ networking/Networking.py | 3 +-- solenoids/Flipper.py | 4 ++-- utils/Singleton.py | 6 ------ utils/__pycache__/Singleton.cpython-38.pyc | Bin 513 -> 0 bytes 12 files changed, 25 insertions(+), 28 deletions(-) delete mode 100644 utils/Singleton.py delete mode 100644 utils/__pycache__/Singleton.cpython-38.pyc diff --git a/InputHandler.py b/InputHandler.py index 773bdeb..0ad9548 100644 --- a/InputHandler.py +++ b/InputHandler.py @@ -1,5 +1,4 @@ -import asyncio -from networking.Networking import Networking +from __main__ import networking class InputHandler: @@ -10,7 +9,7 @@ class InputHandler: async def handleInputs(self): self.isRunning = True while self.isRunning: - event = await Networking().getInputEvent() + event = await networking.getInputEvent() self.targets[event["name"]].hit() #Todo def stop(self): diff --git a/Solenoid.py b/Solenoid.py index 3476620..f937583 100644 --- a/Solenoid.py +++ b/Solenoid.py @@ -1,5 +1,7 @@ from networking.Networking import Networking from utils.util import normalizeName +from __main__ import networking + import logging @@ -14,6 +16,6 @@ class Solenoid: def trigger(self): logging.info("Trigger solenoid with name " + self.name) - Networking().triggerSolenoid(self.name) + networking.triggerSolenoid(self.name) diff --git a/__main__.py b/__main__.py index 6b4b967..8063e06 100644 --- a/__main__.py +++ b/__main__.py @@ -7,6 +7,8 @@ from factories.GameFactory import GameFactory import asyncio import logging +networking = None + def main(): args = parseCommandLine() logging.basicConfig(filename='log.txt', level=logging.INFO) @@ -29,11 +31,12 @@ def defaultOutputServerAddress(): def defaultInputServerAddress(): return config.SOCKET_PATH + config.INPUT_SOCKET_NAME + def createGame(args): print(args) - NetworkFactory.createNetwork(args.OutputServerAddress, args.InputServerAddress) - gameFactory = GameFactory() - game = gameFactory.createGame() + global networking + networking = NetworkFactory.createNetwork(args.OutputServerAddress, args.InputServerAddress) + game = GameFactory().createGame() return game if __name__ == '__main__': diff --git a/displays/Display.py b/displays/Display.py index 560f00e..2d84238 100644 --- a/displays/Display.py +++ b/displays/Display.py @@ -1,9 +1,8 @@ -from networking.Networking import Networking - +from __main__ import networking class Display: def __init__(self, name): self.name = name def printScore(self, score): - Networking().writeDisplayScore(self.name, score) \ No newline at end of file + networking.writeDisplayScore(self.name, score) \ No newline at end of file diff --git a/factories/GameFactory.py b/factories/GameFactory.py index 5de26b7..82c0d99 100644 --- a/factories/GameFactory.py +++ b/factories/GameFactory.py @@ -1,5 +1,4 @@ -from config import * -from networking.Networking import Networking +from __main__ import networking from factories.PlayerStateFactory import PlayerStateFactory from factories.DisplayFactory import DisplayFactory @@ -14,7 +13,7 @@ from Game import Game class GameFactory: def __init__(self): - displayNames = Networking().getDisplays()['displays'] + displayNames = networking.getDisplays()['displays'] self.displayFactory = DisplayFactory(displayNames) specialDisplay = self.displayFactory.createSpecialDisplay() diff --git a/factories/NetworkFactory.py b/factories/NetworkFactory.py index c268b84..054a2aa 100644 --- a/factories/NetworkFactory.py +++ b/factories/NetworkFactory.py @@ -4,4 +4,4 @@ from networking.Networking import Networking class NetworkFactory: @staticmethod def createNetwork(output_server_address, input_server_address): - Networking(output_server_address, input_server_address) \ No newline at end of file + return Networking(output_server_address, input_server_address) \ No newline at end of file diff --git a/lamps/Lamp.py b/lamps/Lamp.py index 4695279..52742a5 100644 --- a/lamps/Lamp.py +++ b/lamps/Lamp.py @@ -1,15 +1,15 @@ -from networking.Networking import Networking +from __main__ import networking class Lamp: def __init__(self, name): self.name = name def activate(self): - Networking().activateLamp(self) + networking.activateLamp(self) def deactivate(self): - Networking().deactivateLamp() + networking.deactivateLamp() def isActivated(self): - Networking().getLamps(self) + networking.getLamps(self) diff --git a/lamps/LampGroup.py b/lamps/LampGroup.py index 5c1c94d..3ac134b 100644 --- a/lamps/LampGroup.py +++ b/lamps/LampGroup.py @@ -1,4 +1,6 @@ from lamps.Lamp import Lamp + + class LampGroup: def __init__(self, lamps): self.lamps = lamps diff --git a/networking/Networking.py b/networking/Networking.py index 05677d7..aef0654 100644 --- a/networking/Networking.py +++ b/networking/Networking.py @@ -2,10 +2,9 @@ import requests_unixsocket as req import socket import json import asyncio -from utils.Singleton import Singleton -class Networking(metaclass=Singleton): +class Networking: def __init__(self, output_server_address, input_socket_address): self.server_address = "" self.input_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) diff --git a/solenoids/Flipper.py b/solenoids/Flipper.py index e3c7041..65eaef6 100644 --- a/solenoids/Flipper.py +++ b/solenoids/Flipper.py @@ -7,8 +7,8 @@ class Flipper: def activate(self): logging.info("Activate flipper with name " + self.name) - Networking().activateFlipper(self.name) + networking.activateFlipper(self.name) def deactivate(self): logging.info("Deactivate flipper with name " + self.name) - Networking().deactivateFlipper(self.name) \ No newline at end of file + networking.deactivateFlipper(self.name) \ No newline at end of file diff --git a/utils/Singleton.py b/utils/Singleton.py deleted file mode 100644 index d6cc857..0000000 --- a/utils/Singleton.py +++ /dev/null @@ -1,6 +0,0 @@ -class Singleton(type): - _instances = {} - def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) - return cls._instances[cls] diff --git a/utils/__pycache__/Singleton.cpython-38.pyc b/utils/__pycache__/Singleton.cpython-38.pyc deleted file mode 100644 index 3625a4cba1925a4d83fb4f6db7a169b6b5c5cf5e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 513 zcmY*Wu};G<5WREUk`^i?)B!fcRtdE~P$4GRx>PI~u%g6NY2CEMiBM6tR_exo^q0J{ zv#>F7msTZQ^v-AB-Fs)}sncl#+5BPf{6*_0Z|Wv#b5F2ca*q&0j2nW6wHIJ4#?bR3 z%KS6$u(Q_`XrMX>L;``gg%^yeYvTuU3lf3`_q*0ONhe0Rre2Aq_&`1{VOZr$xB{ZZQBS(IN>ZDLX{4=d*kY-(j1`=(R1_Mc6sdcW zu}+5B#Jc9}jn^Hz78RJ#TBUaI)9nw6JTZ1~oU^}NW&AQco^6r?Y~v-qU;I@Z6`HJ; zhI6eH&7