Skip to content

Commit ea3fe5d

Browse files
committed
Format with ruff
1 parent 09cddc1 commit ea3fe5d

40 files changed

Lines changed: 2122 additions & 1743 deletions

contrib/dump_comments.py

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,37 @@
4444
from textwrap import indent
4545

4646

47-
class ColorFallback():
48-
__getattr__ = lambda self, name: '' # noqa: E731
47+
class ColorFallback:
48+
__getattr__ = lambda self, name: "" # noqa: E731
4949

5050

5151
try:
5252
from colorama import Fore, Style, init
53+
5354
init() # needed for Windows
5455
except ImportError: # fallback so that the imported classes always exist
5556
Fore = Style = ColorFallback()
5657

5758

58-
Comment = namedtuple('Comment', ('uri', 'id', 'parent', 'created', 'text', 'author', 'email', 'website', 'likes', 'dislikes', 'replies'))
59+
Comment = namedtuple(
60+
"Comment",
61+
(
62+
"uri",
63+
"id",
64+
"parent",
65+
"created",
66+
"text",
67+
"author",
68+
"email",
69+
"website",
70+
"likes",
71+
"dislikes",
72+
"replies",
73+
),
74+
)
5975

60-
INDENT = ' '
61-
QUERY = 'SELECT uri, comments.id, parent, created, text, author, email, website, likes, dislikes FROM comments INNER JOIN threads on comments.tid = threads.id'
76+
INDENT = " "
77+
QUERY = "SELECT uri, comments.id, parent, created, text, author, email, website, likes, dislikes FROM comments INNER JOIN threads on comments.tid = threads.id"
6278

6379

6480
def main():
@@ -78,14 +94,18 @@ def main():
7894
for comment in comments:
7995
if comment.parent: # == this is a "reply" comment
8096
comments_per_id[comment.parent].replies.append(comment)
81-
if args.sort_by_last_reply and (sort_date is None or comment.created > sort_date):
97+
if args.sort_by_last_reply and (
98+
sort_date is None or comment.created > sort_date
99+
):
82100
sort_date = comment.created
83101
else:
84102
root_comments.append(comment)
85103
if sort_date is None or comment.created > sort_date:
86104
sort_date = comment.created
87105
root_comments_per_sort_date[sort_date] = root_comments
88-
for _, root_comments in sorted(root_comments_per_sort_date.items(), key=lambda pair: pair[0]):
106+
for _, root_comments in sorted(
107+
root_comments_per_sort_date.items(), key=lambda pair: pair[0]
108+
):
89109
print(Fore.MAGENTA + args.url_prefix + root_comments[0].uri + Fore.RESET)
90110
for comment in root_comments:
91111
print_comment(INDENT, comment)
@@ -95,34 +115,55 @@ def main():
95115

96116

97117
def print_comment(prefix, comment):
98-
author = comment.author or 'Anonymous'
99-
email = comment.email or ''
100-
website = comment.website or ''
118+
author = comment.author or "Anonymous"
119+
email = comment.email or ""
120+
website = comment.website or ""
101121
when = date.fromtimestamp(comment.created)
102-
popularity = ''
122+
popularity = ""
103123
if comment.likes:
104-
popularity = '+{.likes}'.format(comment)
124+
popularity = "+{.likes}".format(comment)
105125
if comment.dislikes:
106126
if popularity:
107-
popularity += '/'
108-
popularity = '-{.dislikes}'.format(comment)
109-
print(prefix + '{Style.BRIGHT}{author}{Style.RESET_ALL} {Style.DIM}- {email} {website}{Style.RESET_ALL} {when} {Style.DIM}{popularity}{Style.RESET_ALL}'.format(Style=Style, **locals()))
127+
popularity += "/"
128+
popularity = "-{.dislikes}".format(comment)
129+
print(
130+
prefix
131+
+ "{Style.BRIGHT}{author}{Style.RESET_ALL} {Style.DIM}- {email} {website}{Style.RESET_ALL} {when} {Style.DIM}{popularity}{Style.RESET_ALL}".format(
132+
Style=Style, **locals()
133+
)
134+
)
110135
print(indent(comment.text, prefix))
111136

112137

113138
def parse_args():
114-
parser = argparse.ArgumentParser(description='Dump all Isso comments in chronological order, grouped by replies',
115-
formatter_class=ArgparseHelpFormatter)
116-
parser.add_argument('db_path', help='File path to Isso Sqlite DB')
117-
parser.add_argument('--sort-by-last-reply', action='store_true', help='By default comments are sorted by "parent" comment date, this sort comments based on the last replies')
118-
parser.add_argument('--url-prefix', default='', help='Optional domain name to prefix to pages URLs')
119-
parser.add_argument('--no-colors', action='store_false', dest='colors', default=True, help='Disabled colored output')
139+
parser = argparse.ArgumentParser(
140+
description="Dump all Isso comments in chronological order, grouped by replies",
141+
formatter_class=ArgparseHelpFormatter,
142+
)
143+
parser.add_argument("db_path", help="File path to Isso Sqlite DB")
144+
parser.add_argument(
145+
"--sort-by-last-reply",
146+
action="store_true",
147+
help='By default comments are sorted by "parent" comment date, this sort comments based on the last replies',
148+
)
149+
parser.add_argument(
150+
"--url-prefix", default="", help="Optional domain name to prefix to pages URLs"
151+
)
152+
parser.add_argument(
153+
"--no-colors",
154+
action="store_false",
155+
dest="colors",
156+
default=True,
157+
help="Disabled colored output",
158+
)
120159
return parser.parse_args()
121160

122161

123-
class ArgparseHelpFormatter(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
162+
class ArgparseHelpFormatter(
163+
argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter
164+
):
124165
pass
125166

126167

127-
if __name__ == '__main__':
168+
if __name__ == "__main__":
128169
main()

contrib/import_blogger.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
try:
2929
import feedparser
3030
except ImportError:
31-
print("Error: Package feedparser not installed! You can install it via "
32-
"'pip install feedparser' inside your virtualenv.")
31+
print(
32+
"Error: Package feedparser not installed! You can install it via "
33+
"'pip install feedparser' inside your virtualenv."
34+
)
3335
import sys
36+
3437
sys.exit(1)
3538

3639
from urllib.parse import urlparse
@@ -43,21 +46,21 @@ def __init__(self, url):
4346
self.comments = []
4447

4548
def add_comment(self, comment):
46-
comment['id'] = len(self.comments) + 1
49+
comment["id"] = len(self.comments) + 1
4750
self.comments.append(comment)
4851

4952

5053
def encode_post(post):
5154
ret = {}
52-
ret['id'] = post.url
53-
ret['title'] = post.title
54-
ret['comments'] = post.comments
55+
ret["id"] = post.url
56+
ret["title"] = post.title
57+
ret["comments"] = post.comments
5558
return ret
5659

5760

5861
class ImportBlogger:
59-
TYPE_COMMENT = 'http://schemas.google.com/blogger/2008/kind#comment'
60-
TYPE_POST = 'http://schemas.google.com/blogger/2008/kind#post'
62+
TYPE_COMMENT = "http://schemas.google.com/blogger/2008/kind#comment"
63+
TYPE_POST = "http://schemas.google.com/blogger/2008/kind#post"
6164

6265
def __init__(self, filename_in, filename_out, prefix):
6366
self.channel = feedparser.parse(filename_in)
@@ -77,7 +80,7 @@ def run(self):
7780
self.process_post(item)
7881

7982
data = [encode_post(p) for p in self.posts.values() if p.comments]
80-
with open(self.filename_out, 'w') as fp:
83+
with open(self.filename_out, "w") as fp:
8184
json.dump(data, fp, indent=2)
8285

8386
def process_post(self, item):
@@ -99,29 +102,33 @@ def ensure_post(self, item):
99102

100103
def process_comment(self, item):
101104
comment = {}
102-
comment['author'] = item.author_detail.name
103-
comment['email'] = item.author_detail.email
104-
comment['website'] = item.author_detail.get('href', '')
105-
t = time.strftime('%Y-%m-%d %H:%M:%S', item.published_parsed)
106-
comment['created'] = t
107-
comment['text'] = item.content[0].value
108-
comment['remote_addr'] = '127.0.0.1'
105+
comment["author"] = item.author_detail.name
106+
comment["email"] = item.author_detail.email
107+
comment["website"] = item.author_detail.get("href", "")
108+
t = time.strftime("%Y-%m-%d %H:%M:%S", item.published_parsed)
109+
comment["created"] = t
110+
comment["text"] = item.content[0].value
111+
comment["remote_addr"] = "127.0.0.1"
109112
return comment
110113

111114
def post_id(self, item):
112115
u = urlparse(item.link)
113116
return self.prefix + u.path
114117

115118

116-
if __name__ == '__main__':
119+
if __name__ == "__main__":
117120
import argparse
118-
parser = argparse.ArgumentParser(
119-
description='Convert comments from blogger.com')
120-
parser.add_argument('input', help='input file')
121-
parser.add_argument('output', help='output file')
122-
parser.add_argument('-p', dest='prefix',
123-
help='prefix to be added to paths (ID)',
124-
type=str, default='')
121+
122+
parser = argparse.ArgumentParser(description="Convert comments from blogger.com")
123+
parser.add_argument("input", help="input file")
124+
parser.add_argument("output", help="output file")
125+
parser.add_argument(
126+
"-p",
127+
dest="prefix",
128+
help="prefix to be added to paths (ID)",
129+
type=str,
130+
default="",
131+
)
125132
args = parser.parse_args()
126133

127134
importer = ImportBlogger(args.input, args.output, args.prefix)

docs/_extensions/sphinx_reredirects/__init__.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
OPTION_TEMPLATE_FILE = "redirect_html_template_file"
2020
OPTION_TEMPLATE_FILE_DEFAULT = None
2121

22-
REDIRECT_FILE_DEFAULT_TEMPLATE = '<html><head><meta http-equiv="refresh" content="0; url=${to_uri}"></head></html>' # noqa: E501
22+
REDIRECT_FILE_DEFAULT_TEMPLATE = (
23+
'<html><head><meta http-equiv="refresh" content="0; url=${to_uri}"></head></html>' # noqa: E501
24+
)
2325

2426
logger = logging.getLogger(__name__)
2527

@@ -32,13 +34,12 @@ def setup(app: Sphinx):
3234
"""
3335
app.connect("html-collect-pages", init)
3436
app.add_config_value(OPTION_REDIRECTS, OPTION_REDIRECTS_DEFAULT, "env")
35-
app.add_config_value(OPTION_TEMPLATE_FILE, OPTION_TEMPLATE_FILE_DEFAULT,
36-
"env")
37+
app.add_config_value(OPTION_TEMPLATE_FILE, OPTION_TEMPLATE_FILE_DEFAULT, "env")
3738

3839

3940
def init(app: Sphinx):
4041
if not app.config[OPTION_REDIRECTS]:
41-
logger.debug('No redirects configured')
42+
logger.debug("No redirects configured")
4243
return
4344

4445
rr = Reredirects(app)
@@ -53,11 +54,8 @@ def init(app: Sphinx):
5354
class Reredirects:
5455
def __init__(self, app: Sphinx):
5556
self.app = app
56-
self.redirects_option: Dict[str,
57-
str] = getattr(app.config,
58-
OPTION_REDIRECTS)
59-
self.template_file_option: str = getattr(app.config,
60-
OPTION_TEMPLATE_FILE)
57+
self.redirects_option: Dict[str, str] = getattr(app.config, OPTION_REDIRECTS)
58+
self.template_file_option: str = getattr(app.config, OPTION_TEMPLATE_FILE)
6159

6260
def grab_redirects(self) -> Mapping[str, str]:
6361
"""Inspect redirects option in conf.py and returns dict mapping \
@@ -91,17 +89,20 @@ def create_redirects(self, to_be_redirected: Mapping[str, str]):
9189
"""Create actual redirect file for each pair in passed mapping of \
9290
docnames to targets."""
9391
for doc, target in to_be_redirected.items():
94-
redirect_file_abs = Path(
95-
self.app.outdir).joinpath(doc, "index.html")
92+
redirect_file_abs = Path(self.app.outdir).joinpath(doc, "index.html")
9693
redirect_file_rel = redirect_file_abs.relative_to(self.app.outdir)
9794

9895
if redirect_file_abs.exists():
99-
logger.info(f"Creating redirect file '{redirect_file_rel}' "
100-
f"pointing to '{target}' that replaces "
101-
f"document '{doc}'.")
96+
logger.info(
97+
f"Creating redirect file '{redirect_file_rel}' "
98+
f"pointing to '{target}' that replaces "
99+
f"document '{doc}'."
100+
)
102101
else:
103-
logger.info(f"Creating redirect file '{redirect_file_rel}' "
104-
f"pointing to '{target}'.")
102+
logger.info(
103+
f"Creating redirect file '{redirect_file_rel}' "
104+
f"pointing to '{target}'."
105+
)
105106

106107
self._create_redirect_file(redirect_file_abs, target)
107108

@@ -129,8 +130,7 @@ def _render_redirect_template(self, to_uri) -> str:
129130
# HTML used as redirect file content
130131
redirect_template = REDIRECT_FILE_DEFAULT_TEMPLATE
131132
if self.template_file_option:
132-
redirect_file_abs = Path(self.app.srcdir,
133-
self.template_file_option)
133+
redirect_file_abs = Path(self.app.srcdir, self.template_file_option)
134134
redirect_template = redirect_file_abs.read_text()
135135

136136
content = Template(redirect_template).substitute({"to_uri": to_uri})

docs/_theme/remove_heading.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
21
from docutils import nodes
32
from sphinx.writers.html import HTMLTranslator
43

54

65
class IssoTranslator(HTMLTranslator):
7-
86
def visit_title(self, node):
97
if self.section_level == 1:
108
raise nodes.SkipNode

0 commit comments

Comments
 (0)