Skip to content

Commit c494229

Browse files
committed
Blackify code base
1 parent a50d84e commit c494229

16 files changed

+278
-231
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ in progress
1313
- For ``info`` subcommand, add Grafana ``url`` attribute
1414
- Add example how to print the Grafana version using the ``info`` subcommand
1515
- Add more information about dashboard entities to ``info`` subcommand
16+
- Blackify code base
1617

1718
2021-12-10 0.11.1
1819
=================

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ test-coverage: install-tests
4242
--cov-report html:.pytest_results/htmlcov \
4343
--cov-report xml:.pytest_results/coverage.xml
4444

45+
46+
# ----------
47+
# Formatting
48+
# ----------
49+
format: install-releasetools
50+
isort .
51+
black .
52+
53+
4554
# -------
4655
# Release
4756
# -------

doc/backlog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ Done
5656
- [x] Add subcommand ``info``
5757
- Display Grafana version: https://grafana.com/docs/grafana/latest/http_api/other/#health-api
5858
- Display number of dashboards, folders, users, and playlists
59+
- [x] Blackify

grafana_wtf/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""grafana-wtf: Grep through all Grafana entities"""
2-
__appname__ = 'grafana-wtf'
3-
__version__ = '0.11.1'
2+
__appname__ = "grafana-wtf"
3+
__version__ = "0.11.1"

grafana_wtf/commands.py

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
# -*- coding: utf-8 -*-
2-
# (c) 2019 Andreas Motl <[email protected]>
2+
# (c) 2019-2021 Andreas Motl <[email protected]>
33
# License: GNU Affero General Public License, Version 3
4-
import os
54
import json
65
import logging
6+
import os
7+
from collections import OrderedDict
78
from functools import partial
9+
from operator import itemgetter
810
from typing import List
911

12+
from docopt import DocoptExit, docopt
1013
from tabulate import tabulate
11-
from operator import itemgetter
12-
from collections import OrderedDict
13-
from docopt import docopt, DocoptExit
1414

1515
from grafana_wtf import __appname__, __version__
1616
from grafana_wtf.core import GrafanaWtf
1717
from grafana_wtf.report import WtfReport
1818
from grafana_wtf.tabular_report import TabularReport
19-
from grafana_wtf.util import normalize_options, setup_logging, configure_http_logging, read_list, yaml_dump
19+
from grafana_wtf.util import (
20+
configure_http_logging,
21+
normalize_options,
22+
read_list,
23+
setup_logging,
24+
yaml_dump,
25+
)
2026

2127
log = logging.getLogger(__name__)
2228

@@ -126,39 +132,41 @@ def run():
126132
"""
127133

128134
# Parse command line arguments
129-
options = normalize_options(docopt(run.__doc__, version=__appname__ + ' ' + __version__))
135+
options = normalize_options(docopt(run.__doc__, version=f"{__appname__} {__version__}"))
130136

131137
# Setup logging
132-
debug = options.get('debug')
138+
debug = options.get("debug")
133139
log_level = logging.INFO
134140
if debug:
135141
log_level = logging.DEBUG
136142
setup_logging(log_level)
137143

138144
# Debugging
139-
log.debug('Options: {}'.format(json.dumps(options, indent=4)))
145+
log.debug("Options: {}".format(json.dumps(options, indent=4)))
140146

141147
configure_http_logging(options)
142148

143-
grafana_url = options['grafana-url'] or os.getenv('GRAFANA_URL')
144-
grafana_token = options['grafana-token'] or os.getenv('GRAFANA_TOKEN')
149+
grafana_url = options["grafana-url"] or os.getenv("GRAFANA_URL")
150+
grafana_token = options["grafana-token"] or os.getenv("GRAFANA_TOKEN")
145151

146152
# Compute cache expiration time.
147153
try:
148-
cache_ttl = int(options['cache-ttl'])
154+
cache_ttl = int(options["cache-ttl"])
149155
except:
150-
if not options['cache-ttl'] or 'infinite'.startswith(options['cache-ttl'].lower()):
156+
if not options["cache-ttl"] or "infinite".startswith(options["cache-ttl"].lower()):
151157
cache_ttl = None
152158
else:
153159
raise
154160

155161
# Sanity checks
156162
if grafana_url is None:
157-
raise DocoptExit('No Grafana URL given. Please use "--grafana-url" option or environment variable "GRAFANA_URL".')
163+
raise DocoptExit(
164+
'No Grafana URL given. Please use "--grafana-url" option or environment variable "GRAFANA_URL".'
165+
)
158166

159167
engine = GrafanaWtf(grafana_url, grafana_token)
160-
engine.enable_cache(expire_after=cache_ttl, drop_cache=options['drop-cache'])
161-
engine.enable_concurrency(int(options['concurrency']))
168+
engine.enable_cache(expire_after=cache_ttl, drop_cache=options["drop-cache"])
169+
engine.enable_concurrency(int(options["concurrency"]))
162170
engine.setup()
163171

164172
if options.replace:
@@ -195,7 +203,7 @@ def run():
195203
if options.log:
196204
engine.scan_dashboards()
197205
entries = engine.log(dashboard_uid=options.dashboard_uid)
198-
entries = sorted(entries, key=itemgetter('datetime'), reverse=True)
206+
entries = sorted(entries, key=itemgetter("datetime"), reverse=True)
199207

200208
if options.number is not None:
201209
count = int(options.number)
@@ -212,7 +220,7 @@ def run():
212220
output = tabulate(entries, headers="keys", tablefmt=table_format)
213221

214222
else:
215-
raise ValueError(f"Unknown output format \"{output_format}\"")
223+
raise ValueError(f'Unknown output format "{output_format}"')
216224

217225
print(output)
218226

@@ -242,7 +250,7 @@ def output_results(output_format: str, results: List):
242250
output = yaml_dump(results)
243251

244252
else:
245-
raise ValueError(f"Unknown output format \"{output_format}\"")
253+
raise ValueError(f'Unknown output format "{output_format}"')
246254

247255
print(output)
248256

@@ -261,33 +269,37 @@ def get_table_format(output_format):
261269
def to_table(entries):
262270
for entry in entries:
263271
item = entry
264-
name = item['title']
265-
if item['folder']:
266-
name = item['folder'].strip() + ' » ' + name.strip()
267-
item['name'] = name.strip(' 🤓')
268-
#del item['url']
269-
del item['folder']
270-
del item['title']
271-
del item['version']
272+
name = item["title"]
273+
if item["folder"]:
274+
name = item["folder"].strip() + " » " + name.strip()
275+
item["name"] = name.strip(" 🤓")
276+
# del item['url']
277+
del item["folder"]
278+
del item["title"]
279+
del item["version"]
272280
yield item
273281

274282

275283
def compact_table(entries, format):
276-
seperator = '\n'
277-
if format.endswith('pipe'):
278-
seperator = '<br/>'
284+
seperator = "\n"
285+
if format.endswith("pipe"):
286+
seperator = "<br/>"
279287
for entry in entries:
280288
item = OrderedDict()
281-
if format.endswith('pipe'):
282-
link = '[{}]({})'.format(entry['name'], entry['url'])
289+
if format.endswith("pipe"):
290+
link = "[{}]({})".format(entry["name"], entry["url"])
283291
else:
284-
link = 'Name: {}\nURL: {}'.format(entry['name'], entry['url'])
285-
item['Dashboard'] = seperator.join([
286-
'Notes: {}'.format(entry['message'].capitalize() or 'n/a'),
287-
link,
288-
])
289-
item['Update'] = seperator.join([
290-
'User: {}'.format(entry['user']),
291-
'Date: {}'.format(entry['datetime']),
292-
])
292+
link = "Name: {}\nURL: {}".format(entry["name"], entry["url"])
293+
item["Dashboard"] = seperator.join(
294+
[
295+
"Notes: {}".format(entry["message"].capitalize() or "n/a"),
296+
link,
297+
]
298+
)
299+
item["Update"] = seperator.join(
300+
[
301+
"User: {}".format(entry["user"]),
302+
"Date: {}".format(entry["datetime"]),
303+
]
304+
)
293305
yield item

0 commit comments

Comments
 (0)