Update formater in the Makefile with flake8 and autopep8/autoflake

This commit is contained in:
nicolargo 2022-08-20 09:39:49 +02:00
parent ee3f43985e
commit 917f01a830
16 changed files with 119 additions and 74 deletions

21
.flake8 Normal file
View File

@ -0,0 +1,21 @@
[flake8]
ignore =
W504 # line break after binary operator
# --- flake8-bugbear plugin
B007 # Loop control variable 'keyword' not used within the loop body. If this is intended, start the name with an underscore.
B014 # Redundant exception types in `except (IOError, OSError) as err:`. Write `except OSError as err:`, which catches exactly the same exceptions.
B008 # Do not perform function calls in argument defaults.
# --- flake8-blind-except plugin
B902 # blind except Exception: statement
# --- flake8-quotes plugin
Q000 # Double quotes found but single quotes preferred
# --- flake8-quotes naming; disable all except N804 and N805
N801, N802, N803, N806, N807, N811, N812, N813, N814, N815, N816, N817, N818
# lines should not exceed 120 characters
max-line-length = 120

View File

@ -32,6 +32,10 @@ venv-upgrade: venv ## Upgrade Python 3 run-time dependencies
./venv/bin/pip install --upgrade -r requirements.txt
./venv/bin/pip install --upgrade -r optional-requirements.txt
# ===================================================================
# Tests
# ===================================================================
test: venv-upgrade venv-dev-upgrade ## Run unit tests
./venv/bin/python ./unitest.py
./venv/bin/python ./unitest-restful.py
@ -39,9 +43,32 @@ test: venv-upgrade venv-dev-upgrade ## Run unit tests
./venv/bin/python -m black ./glances --check --exclude outputs/static
./venv/bin/pyright glances
# ===================================================================
# Linters and profilers
# ===================================================================
format: venv-dev-upgrade ## Format the code
@git ls-files '*.py' | xargs ./venv/bin/python -m autopep8 --in-place --jobs 0 --global-config=.flake8
@git ls-files '*.py' | xargs ./venv/bin/python -m autoflake --in-place --jobs 0 --remove-all-unused-imports --remove-unused-variables --remove-duplicate-keys
./venv/bin/python -m black ./glances --exclude outputs/static
flake8: venv-dev-upgrade ## Run flake8 linter.
@git ls-files '*.py' | xargs ./venv/bin/python -m flake8 --config=.flake8
profiling: ## How to start the profiling of the Glances software
@echo "Please complete and run: sudo ./venv/bin/py-spy record -o ./docs/_static/glances-flame.svg -d 60 -s --pid <GLANCES PID>"
trace-malloc: ## Trace the malloc() calls
@echo "Malloc test is running, please wait ~30 secondes..."
./venv/bin/python -m glances -C ./conf/glances.conf --trace-malloc --stop-after 15 --quiet
memory-leak: ## Profile memory leaks
./venv/bin/python -m glances -C ./conf/glances.conf --memory-leak
# ===================================================================
# Docs
# ===================================================================
docs: venv-dev-upgrade ## Create the documentation
./venv/bin/python -m glances -C ./conf/glances.conf --api-doc > ./docs/api.rst
cd docs && ./build.sh && cd ..
@ -50,12 +77,35 @@ docs-server: docs ## Start a Web server to serve the documentation
(sleep 2 && sensible-browser "http://localhost:$(PORT)") &
cd docs/_build/html/ && ../../../venv/bin/python -m http.server $(PORT)
release-note: ## Generate release note
git --no-pager log $(LASTTAG)..HEAD --first-parent --pretty=format:"* %s"
@echo "\n"
git --no-pager shortlog -s -n $(LASTTAG)..HEAD
# ===================================================================
# WebUI
# ===================================================================
webui: venv-dev-upgrade ## Build the Web UI
cd glances/outputs/static/ && npm ci && npm run build
webui-audit: venv-dev-upgrade ## Audit the Web UI
cd glances/outputs/static/ && npm audit
# ===================================================================
# Packaging
# ===================================================================
flatpak: venv-dev-upgrade ## Generate FlatPack JSON file
git clone https://github.com/flatpak/flatpak-builder-tools.git
./venv/bin/python ./flatpak-builder-tools/pip/flatpak-pip-generator glances
rm -rf ./flatpak-builder-tools
@echo "Now follow: https://github.com/flathub/flathub/wiki/App-Submission"
# ===================================================================
# Run
# ===================================================================
run: ## Start Glances in console mode (also called standalone)
./venv/bin/python -m glances -C ./conf/glances.conf
@ -83,25 +133,4 @@ show-version: ## Show Glances version number
show-issue: ## Generate output for a new issue
./venv/bin/python -m glances -C ./conf/glances.conf --issue
profiling: ## How to start the profiling of the Glances software
@echo "Please complete and run: sudo ./venv/bin/py-spy record -o ./docs/_static/glances-flame.svg -d 60 -s --pid <GLANCES PID>"
trace-malloc: ## Trace the malloc() calls
@echo "Malloc test is running, please wait ~30 secondes..."
./venv/bin/python -m glances -C ./conf/glances.conf --trace-malloc --stop-after 15 --quiet
memory-leak: ## Profile memory leaks
./venv/bin/python -m glances -C ./conf/glances.conf --memory-leak
release-note: ## Generate release note
git --no-pager log $(LASTTAG)..HEAD --first-parent --pretty=format:"* %s"
@echo "\n"
git --no-pager shortlog -s -n $(LASTTAG)..HEAD
flatpak: venv-dev-upgrade ## Generate FlatPack JSON file
git clone https://github.com/flatpak/flatpak-builder-tools.git
./venv/bin/python ./flatpak-builder-tools/pip/flatpak-pip-generator glances
rm -rf ./flatpak-builder-tools
@echo "Now follow: https://github.com/flathub/flathub/wiki/App-Submission"
.PHONY: test docs docs-server venv

View File

@ -2,3 +2,6 @@ py-spy
black
pyright
requirements-parser
flake8
autopep8
autoflake

View File

@ -349,4 +349,9 @@ def pretty_date(time=False):
def urlopen_auth(url, username, password):
"""Open a url with basic auth"""
return urlopen(Request(url, headers={'Authorization': 'Basic ' + base64.b64encode(('%s:%s' % (username, password)).encode()).decode()}))
return urlopen(
Request(
url,
headers={'Authorization': 'Basic ' + base64.b64encode(('%s:%s' % (username, password)).encode()).decode()},
)
)

View File

@ -31,7 +31,9 @@ class Export(GlancesExport):
self.index = None
# Load the ES configuration file
self.export_enable = self.load_conf('elasticsearch', mandatories=['scheme', 'host', 'port', 'index'], options=[])
self.export_enable = self.load_conf(
'elasticsearch', mandatories=['scheme', 'host', 'port', 'index'], options=[]
)
if not self.export_enable:
sys.exit(2)
@ -46,20 +48,16 @@ class Export(GlancesExport):
try:
es = Elasticsearch(hosts=['{}://{}:{}'.format(self.scheme, self.host, self.port)])
except Exception as e:
logger.critical("Cannot connect to ElasticSearch server %s://%s:%s (%s)" % (self.scheme,
self.host,
self.port, e))
logger.critical(
"Cannot connect to ElasticSearch server %s://%s:%s (%s)" % (self.scheme, self.host, self.port, e)
)
sys.exit(2)
if not es.ping():
logger.critical("Cannot ping the ElasticSearch server %s://%s:%s" % (self.scheme,
self.host,
self.port))
logger.critical("Cannot ping the ElasticSearch server %s://%s:%s" % (self.scheme, self.host, self.port))
sys.exit(2)
else:
logger.info("Connected to the ElasticSearch server %s://%s:%s" % (self.scheme,
self.host,
self.port))
logger.info("Connected to the ElasticSearch server %s://%s:%s" % (self.scheme, self.host, self.port))
return es

View File

@ -35,7 +35,7 @@ class GlancesExport(object):
'ports',
'processlist',
'psutilversion',
'quicklook'
'quicklook',
]
def __init__(self, config=None, args=None):

View File

@ -126,10 +126,7 @@ Examples of use:
help='disable plugin (comma separed list or all). If all is used, then you need to configure --enable-plugin.',
)
parser.add_argument(
'--enable-plugin',
'--enable-plugins',
dest='enable_plugin',
help='enable plugin (comma separed list)'
'--enable-plugin', '--enable-plugins', dest='enable_plugin', help='enable plugin (comma separed list)'
)
parser.add_argument(
'--disable-process',
@ -567,7 +564,9 @@ Examples of use:
logger.critical("'all' key in --disable-plugin needs to be used with --enable-plugin")
sys.exit(2)
else:
logger.info("'all' key in --disable-plugin, only plugins defined with --enable-plugin will be available")
logger.info(
"'all' key in --disable-plugin, only plugins defined with --enable-plugin will be available"
)
if args.disable_plugin is not None:
for p in args.disable_plugin.split(','):
disable(args, p)

View File

@ -24,6 +24,7 @@ from glances.logger import logger
try:
from packaging.version import Version
PACKAGING_IMPORT = True
except Exception as e:
logger.error("Unable to import 'packaging' module ({}). Glances cannot check for updates.".format(e))

View File

@ -97,14 +97,8 @@ another while ensuring that the tasks do not conflict.',
'min_symbol': 'K',
'short_name': 'sys_call',
},
'cpucore': {
'description': 'Total number of CPU core.',
'unit': 'number'
},
'time_since_update': {
'description': 'Number of seconds since last update.',
'unit': 'seconds'
},
'cpucore': {'description': 'Total number of CPU core.', 'unit': 'number'},
'time_since_update': {'description': 'Number of seconds since last update.', 'unit': 'seconds'},
}
# SNMP OID

View File

@ -280,7 +280,9 @@ class Plugin(GlancesPlugin):
# Uptime
container_stats['Uptime'] = pretty_date(
# parser.parse(container.attrs['State']['StartedAt']).replace(tzinfo=None)
parser.parse(container.attrs['State']['StartedAt']).astimezone(tz.tzlocal()).replace(tzinfo=None)
parser.parse(container.attrs['State']['StartedAt'])
.astimezone(tz.tzlocal())
.replace(tzinfo=None)
)
else:
container_stats['cpu'] = {}

View File

@ -113,7 +113,9 @@ class Plugin(GlancesPlugin):
self.view_data['misc_quit'] = msg_col.format('q', 'QUIT (or Esc or Ctrl-C)')
self.view_data['misc_reset_history'] = msg_col.format('r', 'Reset history')
self.view_data['misc_delete_warning_alerts'] = msg_col.format('w', 'Delete warning alerts')
self.view_data['misc_delete_warning_and_critical_alerts'] = msg_col.format('x', 'Delete warning & critical alerts')
self.view_data['misc_delete_warning_and_critical_alerts'] = msg_col.format(
'x', 'Delete warning & critical alerts'
)
self.view_data['misc_edit_process_filter_pattern'] = ' ENTER: Edit process filter pattern'
def get_view_data(self, args=None):

View File

@ -176,8 +176,7 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg_pub, optional=True))
if self.stats['public_info_human']:
ret.append(self.curse_add_line(' {}'.format(self.stats['public_info_human']),
optional=True))
ret.append(self.curse_add_line(' {}'.format(self.stats['public_info_human']), optional=True))
return ret
@ -191,11 +190,7 @@ class Plugin(GlancesPlugin):
field = f.split(':')
if len(field) == 1 and field[0] in public_info:
field_result.append('{}'.format(public_info[field[0]]))
elif (
len(field) == 2
and field[0] in public_info
and field[1] in public_info[field[0]]
):
elif len(field) == 2 and field[0] in public_info and field[1] in public_info[field[0]]:
field_result.append('{}'.format(public_info[field[0]][field[1]]))
return '/'.join(field_result)

View File

@ -37,10 +37,7 @@ waiting in the run-queue plus the number currently executing \
over 15 minutes.',
'unit': 'float',
},
'cpucore': {
'description': 'Total number of CPU core.',
'unit': 'number'
},
'cpucore': {'description': 'Total number of CPU core.', 'unit': 'number'},
}
# SNMP OID

View File

@ -27,9 +27,7 @@ from glances.timer import Counter, Timer
from glances.outputs.glances_unicode import unicode_message
fields_unit_short = {
'percent': '%'
}
fields_unit_short = {'percent': '%'}
fields_unit_type = {
'percent': 'float',
@ -43,7 +41,7 @@ fields_unit_type = {
'second': 'int',
'seconds': 'int',
'byte': 'int',
'bytes': 'int'
'bytes': 'int',
}
@ -847,8 +845,7 @@ class GlancesPlugin(object):
return any(j for j in [re.match(i, value) for i in self.get_conf_value('hide', header=header)])
def is_display(self, value, header=""):
"""Return True if the value should be displayed in the UI
"""
"""Return True if the value should be displayed in the UI"""
if self.get_conf_value('show', header=header) != []:
return self.is_show(value, header=header)
else:
@ -1055,7 +1052,7 @@ class GlancesPlugin(object):
"""
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
if min_symbol in symbols:
symbols = symbols[symbols.index(min_symbol):]
symbols = symbols[symbols.index(min_symbol) :]
prefix = {
'Y': 1208925819614629174706176,
'Z': 1180591620717411303424,
@ -1133,7 +1130,7 @@ class GlancesPlugin(object):
"%s %s %s return %s in %s seconds"
% (
args[0].__class__.__name__,
args[0].__class__.__module__[len('glances_'):],
args[0].__class__.__module__[len('glances_') :],
fct.__name__,
ret,
duration,

View File

@ -430,7 +430,7 @@ class GlancesProcesses(object):
self._sort_key = key
def nice_decrease(self, pid):
""" Decrease nice level
"""Decrease nice level
On UNIX this is a number which usually goes from -20 to 20.
The higher the nice value, the lower the priority of the process."""
p = psutil.Process(pid)
@ -438,10 +438,12 @@ class GlancesProcesses(object):
p.nice(p.nice() - 1)
logger.info('Set nice level of process {} to {} (higher the priority)'.format(pid, p.nice()))
except psutil.AccessDenied:
logger.warning('Can not decrease (higher the priority) the nice level of process {} (access denied)'.format(pid))
logger.warning(
'Can not decrease (higher the priority) the nice level of process {} (access denied)'.format(pid)
)
def nice_increase(self, pid):
""" Increase nice level
"""Increase nice level
On UNIX this is a number which usually goes from -20 to 20.
The higher the nice value, the lower the priority of the process."""
p = psutil.Process(pid)
@ -449,7 +451,9 @@ class GlancesProcesses(object):
p.nice(p.nice() + 1)
logger.info('Set nice level of process {} to {} (lower the priority)'.format(pid, p.nice()))
except psutil.AccessDenied:
logger.warning('Can not increase (lower the priority) the nice level of process {} (access denied)'.format(pid))
logger.warning(
'Can not increase (lower the priority) the nice level of process {} (access denied)'.format(pid)
)
def kill(self, pid, timeout=3):
"""Kill process with pid"""
@ -460,8 +464,6 @@ class GlancesProcesses(object):
return p.wait(timeout)
def weighted(value):
"""Manage None value in dict value."""
return -float('inf') if value is None else value

View File

@ -97,7 +97,7 @@ class GlancesStats(object):
# The key is the plugin name
# for example, the file glances_xxx.py
# generate self._plugins_list["xxx"] = ...
name = plugin_script[len(self.header):-3].lower()
name = plugin_script[len(self.header) : -3].lower()
# Load the plugin class
try: