From 4ec5eaf016bc56843bdb0b2caefdcf3c66a65f20 Mon Sep 17 00:00:00 2001 From: shekhar-cis Date: Thu, 6 Jan 2022 17:32:07 +0530 Subject: [PATCH] Kivy Mock backend added --- src/tests/mock/__init__.py | 0 src/tests/mock/bitmessagemock.py | 32 ++++++++ src/tests/mock/images | 1 + src/tests/mock/kivy_main.py | 8 ++ src/tests/mock/pybitmessage/__init__.py | 0 src/tests/mock/pybitmessage/addresses.py | 1 + src/tests/mock/pybitmessage/bmconfigparser.py | 1 + .../pybitmessage/class_addressGenerator.py | 80 +++++++++++++++++++ src/tests/mock/pybitmessage/inventory.py | 15 ++++ .../mock/pybitmessage/network/__init__.py | 0 .../mock/pybitmessage/network/threads.py | 1 + src/tests/mock/pybitmessage/queues.py | 1 + src/tests/mock/pybitmessage/shutdown.py | 11 +++ src/tests/mock/pybitmessage/singleton.py | 1 + src/tests/mock/pybitmessage/state.py | 1 + 15 files changed, 153 insertions(+) create mode 100644 src/tests/mock/__init__.py create mode 100644 src/tests/mock/bitmessagemock.py create mode 120000 src/tests/mock/images create mode 100644 src/tests/mock/kivy_main.py create mode 100644 src/tests/mock/pybitmessage/__init__.py create mode 120000 src/tests/mock/pybitmessage/addresses.py create mode 120000 src/tests/mock/pybitmessage/bmconfigparser.py create mode 100644 src/tests/mock/pybitmessage/class_addressGenerator.py create mode 100644 src/tests/mock/pybitmessage/inventory.py create mode 100644 src/tests/mock/pybitmessage/network/__init__.py create mode 120000 src/tests/mock/pybitmessage/network/threads.py create mode 120000 src/tests/mock/pybitmessage/queues.py create mode 100644 src/tests/mock/pybitmessage/shutdown.py create mode 120000 src/tests/mock/pybitmessage/singleton.py create mode 120000 src/tests/mock/pybitmessage/state.py diff --git a/src/tests/mock/__init__.py b/src/tests/mock/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/tests/mock/bitmessagemock.py b/src/tests/mock/bitmessagemock.py new file mode 100644 index 00000000..d9ee857b --- /dev/null +++ b/src/tests/mock/bitmessagemock.py @@ -0,0 +1,32 @@ +# pylint: disable=no-name-in-module, import-error + +""" +Bitmessage mock +""" + +from pybitmessage.class_addressGenerator import addressGenerator +from pybitmessage.inventory import Inventory +from pybitmessage.mpybit import NavigateApp +from pybitmessage import state + + +class MockMain(object): # pylint: disable=too-few-public-methods + """Mock main function""" + + def __init__(self): + """Start main application""" + addressGeneratorThread = addressGenerator() + # close the main program even if there are threads left + addressGeneratorThread.start() + Inventory() + state.kivyapp = NavigateApp() + state.kivyapp.run() + + +def main(): + """Triggers main module""" + MockMain() + + +if __name__ == "__main__": + main() diff --git a/src/tests/mock/images b/src/tests/mock/images new file mode 120000 index 00000000..847b03ed --- /dev/null +++ b/src/tests/mock/images @@ -0,0 +1 @@ +../../images/ \ No newline at end of file diff --git a/src/tests/mock/kivy_main.py b/src/tests/mock/kivy_main.py new file mode 100644 index 00000000..79bb413e --- /dev/null +++ b/src/tests/mock/kivy_main.py @@ -0,0 +1,8 @@ +"""Mock kivy app with mock threads.""" +from pybitmessage import state + +if __name__ == '__main__': + state.kivy = True + print("Kivy Loading......") + from bitmessagemock import main + main() diff --git a/src/tests/mock/pybitmessage/__init__.py b/src/tests/mock/pybitmessage/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/tests/mock/pybitmessage/addresses.py b/src/tests/mock/pybitmessage/addresses.py new file mode 120000 index 00000000..88fcee82 --- /dev/null +++ b/src/tests/mock/pybitmessage/addresses.py @@ -0,0 +1 @@ +../../../addresses.py \ No newline at end of file diff --git a/src/tests/mock/pybitmessage/bmconfigparser.py b/src/tests/mock/pybitmessage/bmconfigparser.py new file mode 120000 index 00000000..da05040e --- /dev/null +++ b/src/tests/mock/pybitmessage/bmconfigparser.py @@ -0,0 +1 @@ +../../../bmconfigparser.py \ No newline at end of file diff --git a/src/tests/mock/pybitmessage/class_addressGenerator.py b/src/tests/mock/pybitmessage/class_addressGenerator.py new file mode 100644 index 00000000..34258bbc --- /dev/null +++ b/src/tests/mock/pybitmessage/class_addressGenerator.py @@ -0,0 +1,80 @@ +""" +A thread for creating addresses +""" + +from six.moves import queue + +from pybitmessage import state +from pybitmessage import queues + +from pybitmessage.bmconfigparser import BMConfigParser + +from pybitmessage.network.threads import StoppableThread + + +fake_addresses = { + 'BM-2cUgQGcTLWAkC6dNsv2Bc8XB3Y1GEesVLV': { + 'privsigningkey': '5KWXwYq1oJMzghUSJaJoWPn8VdeBbhDN8zFot1cBd6ezKKReqBd', + 'privencryptionkey': '5JaeFJs8iPcQT3N8676r3gHKvJ5mTWXy1VLhGCEDqRs4vpvpxV8' + }, + 'BM-2cUd2dm8MVMokruMTcGhhteTpyRZCAMhnA': { + 'privsigningkey': '5JnJ79nkcwjo4Aj7iG8sFMkzYoQqWfpUjTcitTuFJZ1YKHZz98J', + 'privencryptionkey': '5JXgNzTRouFLqSRFJvuHMDHCYPBvTeMPBiHt4Jeb6smNjhUNTYq' + }, + 'BM-2cWyvL54WytfALrJHZqbsDHca5QkrtByAW': { + 'privsigningkey': '5KVE4gLmcfYVicLdgyD4GmnbBTFSnY7Yj2UCuytQqgBBsfwDhpi', + 'privencryptionkey': '5JTw48CGm5CP8fyJUJQMq8HQANQMHDHp2ETUe1dgm6EFpT1egD7' + }, + 'BM-2cTE65PK9Y4AQEkCZbazV86pcQACocnRXd': { + 'privsigningkey': '5KCuyReHx9MB4m5hhEyCWcLEXqc8rxhD1T2VWk8CicPFc8B6LaZ', + 'privencryptionkey': '5KBRpwXdX3n2tP7f583SbFgfzgs6Jemx7qfYqhdH7B1Vhe2jqY6' + }, + 'BM-2cX5z1EgmJ87f2oKAwXdv4VQtEVwr2V3BG': { + 'privsigningkey': '5K5UK7qED7F1uWCVsehudQrszLyMZxFVnP6vN2VDQAjtn5qnyRK', + 'privencryptionkey': '5J5coocoJBX6hy5DFTWKtyEgPmADpSwfQTazMpU7QPeART6oMAu' + } +} + + +class addressGenerator(StoppableThread): + """A thread for creating fake addresses""" + name = "addressGenerator" + address_list = list(fake_addresses.keys()) + + def stopThread(self): + """"To stop address generator thread""" + try: + queues.addressGeneratorQueue.put(("stopThread", "data")) + except queue.Full: + self.logger.warning('addressGeneratorQueue is Full') + super(addressGenerator, self).stopThread() # pylint: disable=super-with-arguments + + def run(self): + """ + Process the requests for addresses generation + from `.queues.addressGeneratorQueue` + """ + while state.shutdown == 0: + queueValue = queues.addressGeneratorQueue.get() + try: + address = self.address_list.pop(0) + except IndexError: + self.logger.error( + 'Program error: you can only create 5 fake addresses') + continue + + if len(queueValue) >= 3: + label = queueValue[3] + else: + label = '' + + BMConfigParser().add_section(address) + BMConfigParser().set(address, 'label', label) + BMConfigParser().set(address, 'enabled', 'true') + BMConfigParser().set( + address, 'privsigningkey', fake_addresses[address]['privsigningkey']) + BMConfigParser().set( + address, 'privencryptionkey', fake_addresses[address]['privencryptionkey']) + BMConfigParser().save() + + queues.addressGeneratorQueue.task_done() diff --git a/src/tests/mock/pybitmessage/inventory.py b/src/tests/mock/pybitmessage/inventory.py new file mode 100644 index 00000000..6173c3cd --- /dev/null +++ b/src/tests/mock/pybitmessage/inventory.py @@ -0,0 +1,15 @@ +"""The Inventory singleton""" + +# TODO make this dynamic, and watch out for frozen, like with messagetypes +from pybitmessage.singleton import Singleton + + +# pylint: disable=old-style-class,too-few-public-methods +@Singleton +class Inventory(): + """ + Inventory singleton class which uses storage backends + to manage the inventory. + """ + def __init__(self): + self.numberOfInventoryLookupsPerformed = 0 diff --git a/src/tests/mock/pybitmessage/network/__init__.py b/src/tests/mock/pybitmessage/network/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/tests/mock/pybitmessage/network/threads.py b/src/tests/mock/pybitmessage/network/threads.py new file mode 120000 index 00000000..c95b4c36 --- /dev/null +++ b/src/tests/mock/pybitmessage/network/threads.py @@ -0,0 +1 @@ +../../../../network/threads.py \ No newline at end of file diff --git a/src/tests/mock/pybitmessage/queues.py b/src/tests/mock/pybitmessage/queues.py new file mode 120000 index 00000000..8c556015 --- /dev/null +++ b/src/tests/mock/pybitmessage/queues.py @@ -0,0 +1 @@ +../../../queues.py \ No newline at end of file diff --git a/src/tests/mock/pybitmessage/shutdown.py b/src/tests/mock/pybitmessage/shutdown.py new file mode 100644 index 00000000..08c885d8 --- /dev/null +++ b/src/tests/mock/pybitmessage/shutdown.py @@ -0,0 +1,11 @@ +# pylint: disable=invalid-name +"""shutdown function""" + +from pybitmessage import state + + +def doCleanShutdown(): + """ + Used to exit Kivy UI. + """ + state.shutdown = 1 diff --git a/src/tests/mock/pybitmessage/singleton.py b/src/tests/mock/pybitmessage/singleton.py new file mode 120000 index 00000000..5e112567 --- /dev/null +++ b/src/tests/mock/pybitmessage/singleton.py @@ -0,0 +1 @@ +../../../singleton.py \ No newline at end of file diff --git a/src/tests/mock/pybitmessage/state.py b/src/tests/mock/pybitmessage/state.py new file mode 120000 index 00000000..117203f5 --- /dev/null +++ b/src/tests/mock/pybitmessage/state.py @@ -0,0 +1 @@ +../../../state.py \ No newline at end of file