Skip to content

Commit

Permalink
Merge pull request #43 from Fuzzbawls/2021_flake8-cleanup
Browse files Browse the repository at this point in the history
[Cleanup] Code cleanup per PEP8/Flake8 standards
  • Loading branch information
random-zebra authored Feb 22, 2021
2 parents 116cfec + 2ca6228 commit 6eacc7d
Show file tree
Hide file tree
Showing 50 changed files with 742 additions and 1,109 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: CI Actions for SPMT

on: [push, pull_request]
jobs:
lint:
name: Lint
runs-on: ubuntu-16.04
defaults:
run:
shell: bash
steps:
- name: Checkout Repo
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Initialize Python
uses: actions/setup-python@v2
with:
python-version: 3.7

- name: Install Dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
pip install -r requirements.txt
pip install flake8==3.8.4
pip install mypy==0.781
pip install vulture==2.3
- name: Lint
run: |
lint/lint-python.sh
lint/lint-python-mutable-default-parameters.sh
lint/lint-python-utf8-encoding.sh
build:
name: Build-${{ matrix.config.name }}
runs-on: ${{ matrix.config.os }}
needs: lint
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
config:
- name: Linux
os: ubuntu-16.04
cachepath: ~/.cache/pip
packages: libusb-1.0-0-dev libudev-dev

- name: macOS
os: macos-10.15
cachepath: ~/Library/Caches/pip

- name: Windows
os: windows-2019
cachepath: ~\AppData\Local\pip\Cache

steps:
- name: Get Source
uses: actions/checkout@v2
- name: Setup Python 3.7
uses: actions/setup-python@v2
with:
python-version: 3.7
- name: Setup pip cache
uses: actions/cache@v2
with:
path: ${{ matrix.config.cachepath }}
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
if [[ ${{ matrix.config.os }} = ubuntu* ]]; then
sudo apt-get update
sudo apt-get install --no-install-recommends --no-upgrade -qq ${{ matrix.config.packages }}
fi
python -m pip install --upgrade pip setuptools wheel
pip install -r requirements.txt
pip install pyinstaller
- name: Build
run: |
pyinstaller SecurePivxMasternodeTool.spec
49 changes: 49 additions & 0 deletions lint/lint-python-mutable-default-parameters.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
#
# Copyright (c) 2019 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
# Detect when a mutable list or dict is used as a default parameter value in a Python function.

export LC_ALL=C
EXIT_CODE=0
OUTPUT=$(git grep -E '^\s*def [a-zA-Z0-9_]+\(.*=\s*(\[|\{)' -- "*.py")
if [[ ${OUTPUT} != "" ]]; then
echo "A mutable list or dict seems to be used as default parameter value:"
echo
echo "${OUTPUT}"
echo
cat << EXAMPLE
This is how mutable list and dict default parameter values behave:
>>> def f(i, j=[], k={}):
... j.append(i)
... k[i] = True
... return j, k
...
>>> f(1)
([1], {1: True})
>>> f(1)
([1, 1], {1: True})
>>> f(2)
([1, 1, 2], {1: True, 2: True})
The intended behaviour was likely:
>>> def f(i, j=None, k=None):
... if j is None:
... j = []
... if k is None:
... k = {}
... j.append(i)
... k[i] = True
... return j, k
...
>>> f(1)
([1], {1: True})
>>> f(1)
([1], {1: True})
>>> f(2)
([2], {2: True})
EXAMPLE
EXIT_CODE=1
fi
exit ${EXIT_CODE}
28 changes: 28 additions & 0 deletions lint/lint-python-utf8-encoding.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
#
# Copyright (c) 2018-2020 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
# Make sure we explicitly open all text files using UTF-8 (or ASCII) encoding to
# avoid potential issues on the BSDs where the locale is not always set.

export LC_ALL=C
EXIT_CODE=0
OUTPUT=$(git grep " open(" -- "*.py" | grep -vE "encoding=.(ascii|utf8|utf-8)." | grep -vE "open\([^,]*, ['\"][^'\"]*b[^'\"]*['\"]")
if [[ ${OUTPUT} != "" ]]; then
echo "Python's open(...) seems to be used to open text files without explicitly"
echo "specifying encoding=\"utf8\":"
echo
echo "${OUTPUT}"
EXIT_CODE=1
fi
OUTPUT=$(git grep "check_output(" -- "*.py" | grep "universal_newlines=True" | grep -vE "encoding=.(ascii|utf8|utf-8).")
if [[ ${OUTPUT} != "" ]]; then
echo "Python's check_output(...) seems to be used to get program outputs without explicitly"
echo "specifying encoding=\"utf8\":"
echo
echo "${OUTPUT}"
EXIT_CODE=1
fi
exit ${EXIT_CODE}
109 changes: 109 additions & 0 deletions lint/lint-python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env bash
#
# Copyright (c) 2017-2020 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
# Check for specified flake8 warnings in python files.

export LC_ALL=C
export MYPY_CACHE_DIR="${BASE_ROOT_DIR}/test/.mypy_cache"

enabled=(
E101 # indentation contains mixed spaces and tabs
E112 # expected an indented block
E113 # unexpected indentation
E115 # expected an indented block (comment)
E116 # unexpected indentation (comment)
E125 # continuation line with same indent as next logical line
E129 # visually indented line with same indent as next logical line
E131 # continuation line unaligned for hanging indent
E133 # closing bracket is missing indentation
E223 # tab before operator
E224 # tab after operator
E242 # tab after ','
E266 # too many leading '#' for block comment
E271 # multiple spaces after keyword
E272 # multiple spaces before keyword
E273 # tab after keyword
E274 # tab before keyword
E275 # missing whitespace after keyword
E304 # blank lines found after function decorator
E306 # expected 1 blank line before a nested definition
E401 # multiple imports on one line
E402 # module level import not at top of file
E502 # the backslash is redundant between brackets
E701 # multiple statements on one line (colon)
E702 # multiple statements on one line (semicolon)
E703 # statement ends with a semicolon
E711 # comparison to None should be 'if cond is None:'
E714 # test for object identity should be "is not"
E721 # do not compare types, use "isinstance()"
E742 # do not define classes named "l", "O", or "I"
E743 # do not define functions named "l", "O", or "I"
E901 # SyntaxError: invalid syntax
E902 # TokenError: EOF in multi-line string
F401 # module imported but unused
F402 # import module from line N shadowed by loop variable
F403 # 'from foo_module import *' used; unable to detect undefined names
F404 # future import(s) name after other statements
F405 # foo_function may be undefined, or defined from star imports: bar_module
F406 # "from module import *" only allowed at module level
F407 # an undefined __future__ feature name was imported
F601 # dictionary key name repeated with different values
F602 # dictionary key variable name repeated with different values
F621 # too many expressions in an assignment with star-unpacking
F622 # two or more starred expressions in an assignment (a, *b, *c = d)
F631 # assertion test is a tuple, which are always True
F632 # use ==/!= to compare str, bytes, and int literals
F701 # a break statement outside of a while or for loop
F702 # a continue statement outside of a while or for loop
F703 # a continue statement in a finally block in a loop
F704 # a yield or yield from statement outside of a function
F705 # a return statement with arguments inside a generator
F706 # a return statement outside of a function/method
F707 # an except: block as not the last exception handler
F811 # redefinition of unused name from line N
F812 # list comprehension redefines 'foo' from line N
F821 # undefined name 'Foo'
F822 # undefined name name in __all__
F823 # local variable name … referenced before assignment
F831 # duplicate argument name in function definition
F841 # local variable 'foo' is assigned to but never used
W191 # indentation contains tabs
W291 # trailing whitespace
W292 # no newline at end of file
W293 # blank line contains whitespace
W601 # .has_key() is deprecated, use "in"
W602 # deprecated form of raising exception
W603 # "<>" is deprecated, use "!="
W604 # backticks are deprecated, use "repr()"
W605 # invalid escape sequence "x"
W606 # 'async' and 'await' are reserved keywords starting with Python 3.7
)

if ! command -v flake8 > /dev/null; then
echo "Skipping Python linting since flake8 is not installed."
exit 0
elif PYTHONWARNINGS="ignore" flake8 --version | grep -q "Python 2"; then
echo "Skipping Python linting since flake8 is running under Python 2. Install the Python 3 version of flake8."
exit 0
fi

EXIT_CODE=0

if ! PYTHONWARNINGS="ignore" flake8 --ignore=B,C,E,F,I,N,W --select=$(IFS=","; echo "${enabled[*]}") $(
if [[ $# == 0 ]]; then
git ls-files "*.py"
else
echo "$@"
fi
); then
EXIT_CODE=1
fi

if ! mypy --ignore-missing-imports $(git ls-files "src/*.py"); then
EXIT_CODE=1
fi

exit $EXIT_CODE
42 changes: 19 additions & 23 deletions spmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import os
import sys
import time

if __name__ == '__main__':

# parse input if there's `--clear[?]Data` flags
import argparse
parser = argparse.ArgumentParser(description='Secure PIVX Masternode Tool')
Expand All @@ -27,12 +27,12 @@
parser.set_defaults(clearRpcData=False)
parser.set_defaults(clearTxCache=False)
args = parser.parse_args()

if getattr( sys, 'frozen', False ) :
# running in a bundle
sys.path.append(os.path.join(sys._MEIPASS, 'src'))
imgDir = os.path.join(sys._MEIPASS, 'img')

# if linux export qt plugins path
if sys.platform == 'linux':
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = os.path.join(sys._MEIPASS, 'PyQt5', 'Qt', 'plugins')
Expand All @@ -41,24 +41,24 @@
# running live
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src'))
imgDir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'img')

from PyQt5.QtWidgets import QApplication
from PyQt5.Qt import Qt, QPixmap, QSplashScreen, QProgressBar, QColor, QPalette, QLabel
from PyQt5.Qt import Qt, QPixmap, QSplashScreen, QProgressBar, QLabel

from misc import updateSplash
from spmtApp import App
from spmtApp import App

# Create App
app = QApplication(sys.argv)
### -- style stuff

# -- style stuff
spmtLogo_file = os.path.join(imgDir, 'splashscreen.png')
labelstyle = "QLabel { font-size: 14px; color: purple; font-style: italic; text-align: center;}"
barStyle = "QProgressBar::chunk {background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #372f43,stop: 0.6 #5c4c7a, stop: 0.8 #663399);border-bottom-right-radius: 7px;border-bottom-left-radius: 7px;border-top: 2px solid #8A2BE2;}"

splash_pix = QPixmap(spmtLogo_file)
splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)

progressBar = QProgressBar(splash)
progressBar.setGeometry(0, splash_pix.height()-13, splash_pix.width(), 13)
progressBar.setStyleSheet(barStyle)
Expand All @@ -68,10 +68,10 @@
label.setStyleSheet(labelstyle)
label.setGeometry((splash_pix.width()-500)/2, splash_pix.height()-40, 500, 20)
label.setAlignment(Qt.AlignCenter)

progressText = "loading..."
label.setText(progressText)

splash.show()

for i in range(0, 100):
Expand All @@ -81,17 +81,15 @@
t = time.time()
while time.time() < t + 0.01:
app.processEvents()

### --------------


# Create QMainWindow Widget
ex = App(imgDir, app, args)

# Close Splashscreen
splash.close()

try:
##-- Launch RPC watchdog
# -- Launch RPC watchdog
ex.mainWindow.rpc_watchdogThread.start()
except Exception as e:
print(e)
Expand All @@ -102,7 +100,5 @@
app.deleteLater()
except Exception as e:
print(e)

sys.exit()


Loading

0 comments on commit 6eacc7d

Please sign in to comment.