From 69f8d50f81bb47efc70f728ebaddcff7b8120742 Mon Sep 17 00:00:00 2001 From: lakshyacis Date: Tue, 17 Sep 2019 20:14:26 +0530 Subject: [PATCH] code fixes for PR#11 --- src/bitmessagekivy/mpybit.py | 8 ++-- src/bitmessagemain.py | 76 +++++++++++++++++++++--------------- src/identiconGeneration.py | 16 ++++++-- src/pyelliptic/openssl.py | 41 ++++++++++++------- 4 files changed, 90 insertions(+), 51 deletions(-) diff --git a/src/bitmessagekivy/mpybit.py b/src/bitmessagekivy/mpybit.py index be4be868..e3225c8c 100644 --- a/src/bitmessagekivy/mpybit.py +++ b/src/bitmessagekivy/mpybit.py @@ -1,5 +1,7 @@ -"""Coding: utf-8.""" -# pylint: disable=relative-import, too-many-lines +""" +src/bitmessagekivy/mpybit.py +================================= +""" import os import time from functools import partial @@ -53,7 +55,7 @@ import queues from semaphores import kivyuisignaler import state from uikivysignaler import UIkivySignaler -# pylint: disable=unused-argument, too-few-public-methods +# pylint: disable=unused-argument, too-few-public-methods, import-error if platform == 'linux': diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 6d80ced0..7a8fc475 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -1,4 +1,8 @@ -#!/usr/bin/python2.7 +""" +src/bitmessagemain.py +================================= +""" +# !/usr/bin/python2.7 # Copyright (c) 2012-2016 Jonathan Warren # Copyright (c) 2012-2019 The Bitmessage developers # Distributed under the MIT/X11 software license. See the accompanying @@ -11,16 +15,6 @@ import os import sys - -app_dir = os.path.dirname(os.path.abspath(__file__)) -os.chdir(app_dir) -sys.path.insert(0, app_dir) - - -import depends - -depends.check_dependencies() - import ctypes import getopt import multiprocessing @@ -38,6 +32,7 @@ from helper_startup import ( from singleinstance import singleinstance import defaults +import depends import shared import knownnodes import state @@ -67,8 +62,15 @@ from network.uploadthread import UploadThread # Helper Functions import helper_threading +app_dir = os.path.dirname(os.path.abspath(__file__)) +os.chdir(app_dir) +sys.path.insert(0, app_dir) + +depends.check_dependencies() + def connectToStream(streamNumber): + """Connecting to stream""" state.streamsInWhichIAmParticipating.append(streamNumber) if isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): @@ -109,6 +111,7 @@ def _fixSocket(): addressToString = ctypes.windll.ws2_32.WSAAddressToStringA def inet_ntop(family, host): + """inet ntop""" if family == socket.AF_INET: if len(host) != 4: raise ValueError("invalid IPv4 host") @@ -130,6 +133,7 @@ def _fixSocket(): stringToAddress = ctypes.windll.ws2_32.WSAStringToAddressA def inet_pton(family, host): + """inet pton""" buf = "\0" * 28 lengthBuf = pack("I", len(buf)) if stringToAddress(str(host), @@ -175,16 +179,18 @@ def signal_handler(signum, frame): if shared.thisapp.daemon or not state.enableGUI: shutdown.doCleanShutdown() else: - print('# Thread: %s(%d)' % (thread.name, thread.ident)) + print '# Thread: %s(%d)' % (thread.name, thread.ident) for filename, lineno, name, line in traceback.extract_stack(frame): - print('File: "%s", line %d, in %s' % (filename, lineno, name)) + print 'File: "%s", line %d, in %s' % (filename, lineno, name) if line: - print(' %s' % line.strip()) - print('Unfortunately you cannot use Ctrl+C when running the UI' - ' because the UI captures the signal.') + print ' %s' % line.strip() + print 'Unfortunately you cannot use Ctrl+C when running the UI \ + because the UI captures the signal.' -class Main: +class Main: # pylint: disable=no-init, old-style-class + """Main Method""" + @staticmethod def start_proxyconfig(config): """Check socksproxytype and start any proxy configuration plugin""" @@ -206,7 +212,8 @@ class Main: 'Started proxy config plugin %s in %s sec', proxy_type, time.time() - proxyconfig_start) - def start(self): + def start(self): # pylint: disable=too-many-statements, too-many-branches, too-many-locals + """Start the main method""" _fixSocket() config = BMConfigParser() @@ -274,7 +281,7 @@ class Main: if daemon: with shared.printLock: - print('Running as a daemon. Send TERM signal to end.') + print 'Running as a daemon. Send TERM signal to end.' self.daemonize() self.setSignalHandler() @@ -397,7 +404,7 @@ class Main: if state.curses: if not depends.check_curses(): sys.exit() - print('Running with curses') + print 'Running with curses' import bitmessagecurses bitmessagecurses.runwrapper() @@ -415,8 +422,7 @@ class Main: if daemon: while state.shutdown == 0: time.sleep(1) - if ( - state.testmode and time.time() - state.last_api_response >= 30): + if (state.testmode and time.time() - state.last_api_response >= 30): self.stop() elif not state.enableGUI: from tests import core as test_core # pylint: disable=relative-import @@ -430,7 +436,9 @@ class Main: else 0 ) - def daemonize(self): + @staticmethod + def daemonize(): + """Daemonize""" grandfatherPid = os.getpid() parentPid = None try: @@ -440,7 +448,7 @@ class Main: # wait until grandchild ready while True: time.sleep(1) - os._exit(0) + os._exit(0) # pylint: disable=protected-access except AttributeError: # fork not implemented pass @@ -461,7 +469,7 @@ class Main: # wait until child ready while True: time.sleep(1) - os._exit(0) + os._exit(0) # pylint: disable=protected-access except AttributeError: # fork not implemented pass @@ -482,12 +490,15 @@ class Main: os.kill(parentPid, signal.SIGTERM) os.kill(grandfatherPid, signal.SIGTERM) - def setSignalHandler(self): + def setSignalHandler(self): # pylint: disable=no-self-use + """Set Signal Handler""" signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) # signal.signal(signal.SIGINT, signal.SIG_DFL) - def usage(self): + @staticmethod + def usage(): + """getting usages""" print 'Usage: ' + sys.argv[0] + ' [OPTIONS]' print ''' Options: @@ -499,13 +510,15 @@ Options: All parameters are optional. ''' - def stop(self): + def stop(self): # pylint: disable=no-self-use + """Stopping Bitmessage Deamon""" with shared.printLock: - print('Stopping Bitmessage Deamon.') + print 'Stopping Bitmessage Deamon.' shutdown.doCleanShutdown() - # TODO: nice function but no one is using this - def getApiAddress(self): + # ..todo: nice function but no one is using this + def getApiAddress(self): # pylint: disable=no-self-use + """Getting Api Addresses""" if not BMConfigParser().safeGetBoolean( 'bitmessagesettings', 'apienabled'): return None @@ -515,6 +528,7 @@ All parameters are optional. def main(): + """Start of main thread""" mainprogram = Main() mainprogram.start() diff --git a/src/identiconGeneration.py b/src/identiconGeneration.py index 2465c030..dfafbfb7 100644 --- a/src/identiconGeneration.py +++ b/src/identiconGeneration.py @@ -1,9 +1,15 @@ +""" +src/identiconGeneration +================================= +""" import hashlib from PIL import Image from kivy.core.image import Image as CoreImage -# Core classes for loading images and converting them to a Texture. The raw image data can be keep in memory for further access from kivy.uix.image import Image as kiImage from io import BytesIO +""" Core classes for loading images and converting them to a Texture. +The raw image data can be keep in memory for further access """ + # constants RESOLUTION = 128, 128 @@ -13,6 +19,7 @@ MODE = "RGB" def generate(Generate_string=None): + """Generating string""" hash_string = generate_hash(Generate_string) color = random_color(hash_string) image = Image.new(MODE, V_RESOLUTION, BACKGROUND_COLOR) @@ -32,20 +39,22 @@ def generate(Generate_string=None): def generate_hash(string): + """Generating hash""" try: # make input case insensitive string = str.lower(string) hash_object = hashlib.md5(str.encode(string)) - print(hash_object.hexdigest()) + print hash_object.hexdigest() # returned object is a hex string return hash_object.hexdigest() except IndexError: - print("Error: Please enter a string as an argument.") + print "Error: Please enter a string as an argument." def random_color(hash_string): + """Getting random color""" # remove first three digits from hex string split = 6 rgb = hash_string[:split] @@ -62,6 +71,7 @@ def random_color(hash_string): def generate_image(image, color, hash_string): + """Generating images""" hash_string = hash_string[6:] lower_x = 1 diff --git a/src/pyelliptic/openssl.py b/src/pyelliptic/openssl.py index 7fb78233..c7048dac 100644 --- a/src/pyelliptic/openssl.py +++ b/src/pyelliptic/openssl.py @@ -1,4 +1,15 @@ -#!/usr/bin/env python +""" +src/pyelliptic/openssl.py +================================= +""" + +import sys +import ctypes +from kivy.utils import platform + +OpenSSL = None + +# !/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2011 Yann GUIBET @@ -6,13 +17,9 @@ # # Software slightly changed by Jonathan Warren -import sys -import ctypes -OpenSSL = None -from kivy.utils import platform - -class CipherName: +class CipherName: # pylint: disable=old-style-class + """Getting CipherName""" def __init__(self, name, pointer, blocksize): self._name = name self._pointer = pointer @@ -24,16 +31,20 @@ class CipherName: " | Function pointer : " + str(self._pointer) def get_pointer(self): + """Getting pointer""" return self._pointer() def get_name(self): + """Getting Name""" return self._name def get_blocksize(self): + """Getting blocksize""" return self._blocksize def get_version(library): + """Getting versions""" version = None hexversion = None cflags = None @@ -64,14 +75,11 @@ def get_version(library): return (version, hexversion, cflags) -class _OpenSSL: - """ - Wrapper for OpenSSL using ctypes - """ +class _OpenSSL: # pylint: disable=too-many-instance-attributes, old-style-class, too-many-statements + """Wrapper for OpenSSL using ctypes""" + def __init__(self, library): - """ - Build the wrapper - """ + """Build the wrapper""" self._lib = ctypes.CDLL(library) self._version, self._hexversion, self._cflags = get_version(self._lib) self._libreSSL = self._version.startswith("LibreSSL") @@ -594,6 +602,7 @@ class _OpenSSL: """ returns the name of a elliptic curve with his id """ + # pylint: disable=redefined-builtin res = None for i in self.curves: if self.curves[i] == id: @@ -607,6 +616,7 @@ class _OpenSSL: """ OpenSSL random function """ + # pylint: disable=redefined-builtin buffer = self.malloc(0, size) # This pyelliptic library, by default, didn't check the return value of RAND_bytes. It is # evidently possible that it returned an error and not-actually-random data. However, in @@ -623,6 +633,7 @@ class _OpenSSL: """ returns a create_string_buffer (ctypes) """ + # pylint: disable=redefined-builtin buffer = None if data != 0: if sys.version_info.major == 3 and isinstance(data, type('')): @@ -634,6 +645,8 @@ class _OpenSSL: def loadOpenSSL(): + """Loading OpenSSL""" + # pylint: disable=global-statement, protected-access, too-many-branches global OpenSSL from os import path, environ from ctypes.util import find_library