Add common-backend and separate addressgenerator method from login.py

This commit is contained in:
shekhar-cis 2022-06-28 16:40:27 +05:30
parent a2c9390398
commit a721ee2d5b
Signed by untrusted user: shekhar-cis
GPG Key ID: F4F00AB04E83F9A7
2 changed files with 69 additions and 28 deletions

View File

@ -1,4 +1,4 @@
import queues # import queues
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
from kivy.clock import Clock from kivy.clock import Clock
@ -7,8 +7,8 @@ from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.behaviors.elevation import RectangularElevationBehavior from kivymd.uix.behaviors.elevation import RectangularElevationBehavior
from kivy.uix.screenmanager import Screen from kivy.uix.screenmanager import Screen
from kivy.app import App from kivy.app import App
from common_backend import AddressGenerator
import state # import state
from bitmessagekivy.baseclass.common import toast from bitmessagekivy.baseclass.common import toast
@ -43,18 +43,19 @@ class Random(Screen):
entered_label = str(self.ids.add_random_bx.children[0].ids.lab.text).strip() entered_label = str(self.ids.add_random_bx.children[0].ids.lab.text).strip()
if not entered_label: if not entered_label:
self.ids.add_random_bx.children[0].ids.lab.focus = True self.ids.add_random_bx.children[0].ids.lab.focus = True
streamNumberForAddress = 1 is_address = AddressGenerator.generate_address(entered_label)
eighteenByteRipe = False # streamNumberForAddress = 1
nonceTrialsPerByte = 1000 # eighteenByteRipe = False
payloadLengthExtraBytes = 1000 # nonceTrialsPerByte = 1000
lables = [BMConfigParser().get(obj, 'label') # payloadLengthExtraBytes = 1000
for obj in BMConfigParser().addresses()] # labels = [BMConfigParser().get(obj, 'label')
if entered_label and entered_label not in lables: # for obj in BMConfigParser().addresses()]
if is_address:
toast('Address Creating...') toast('Address Creating...')
queues.addressGeneratorQueue.put(( # queues.addressGeneratorQueue.put((
'createRandomAddress', 4, streamNumberForAddress, entered_label, 1, # 'createRandomAddress', 4, streamNumberForAddress, entered_label, 1,
"", eighteenByteRipe, nonceTrialsPerByte, # "", eighteenByteRipe, nonceTrialsPerByte,
payloadLengthExtraBytes)) # payloadLengthExtraBytes))
self.parent.parent.ids.toolbar.opacity = 1 self.parent.parent.ids.toolbar.opacity = 1
self.parent.parent.ids.toolbar.disabled = False self.parent.parent.ids.toolbar.disabled = False
App.get_running_app().loadMyAddressScreen(True) App.get_running_app().loadMyAddressScreen(True)
@ -81,21 +82,21 @@ class Random(Screen):
@staticmethod @staticmethod
def add_validation(instance): def add_validation(instance):
"""Checking validation at address creation time""" """Retrieve created labels and validate"""
entered_label = str(instance.text.strip()) entered_label = str(instance.text.strip())
lables = [BMConfigParser().get(obj, 'label') # labels = [BMConfigParser().get(obj, 'label')
for obj in BMConfigParser().addresses()] # for obj in BMConfigParser().addresses()]
if entered_label in lables: AddressGenerator.address_validation(instance, entered_label)
instance.error = True # if entered_label in lables:
instance.helper_text = 'it is already exist you'\ # instance.error = True
' can try this Ex. ( {0}_1, {0}_2 )'.format( # instance.helper_text = 'it is already exist you'\
entered_label) # ' can try this Ex. ( {0}_1, {0}_2 )'.format(
elif entered_label: # entered_label)
instance.error = False # elif entered_label:
else: # instance.error = False
instance.error = False # else:
instance.helper_text = 'This field is required' # instance.error = False
# instance.helper_text = 'This field is required'
def reset_address_label(self): def reset_address_label(self):
"""Resetting address labels""" """Resetting address labels"""
if not self.ids.add_random_bx.children: if not self.ids.add_random_bx.children:

40
src/common_backend.py Normal file
View File

@ -0,0 +1,40 @@
import queues
from bmconfigparser import BMConfigParser
from bitmessagekivy.baseclass.common import toast
class AddressGenerator:
@staticmethod
def generate_address(entered_label):
""""Return True if the label is uniqe"""
streamNumberForAddress = 1
eighteenByteRipe = False
nonceTrialsPerByte = 1000
payloadLengthExtraBytes = 1000
labels = [BMConfigParser().get(obj, 'label')
for obj in BMConfigParser().addresses()]
if entered_label and entered_label not in labels:
toast('Address Creating...')
queues.addressGeneratorQueue.put((
'createRandomAddress', 4, streamNumberForAddress, entered_label, 1,
"", eighteenByteRipe, nonceTrialsPerByte,
payloadLengthExtraBytes))
return True
return False
@staticmethod
def address_validation(instance, entered_label):
"""Checking address validation while creating"""
labels = [BMConfigParser().get(obj, 'label')
for obj in BMConfigParser().addresses()]
if entered_label in labels:
instance.error = True
instance.helper_text = 'it is already exist you'\
' can try this Ex. ( {0}_1, {0}_2 )'.format(
entered_label)
elif entered_label:
instance.error = False
else:
instance.error = False
instance.helper_text = 'This field is required'