Skip to content

Sourcery refactored master branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ def header_link(title):
for character in title:
if character in string.whitespace:
result += '-'
elif character in string.punctuation:
pass
else:
elif character not in string.punctuation:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function header_link refactored with the following changes:

result += character.lower()
return result

Expand All @@ -104,13 +102,7 @@ def askyesno(question, default=True):
The default answer is yes if default is True and no if default is
False.
"""
if default:
# yes by default
question += ' [Y/n] '
else:
# no by default
question += ' [y/N] '

question += ' [Y/n] ' if default else ' [y/N] '
Comment on lines -107 to +105
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function askyesno refactored with the following changes:

This removes the following comments ( why? ):

# yes by default
# no by default

while True:
result = input(question).upper().strip()
if result == 'Y':
Expand All @@ -125,15 +117,15 @@ def askyesno(question, default=True):
@contextlib.contextmanager
def backup(filename):
"""A context manager that backs up a file."""
shutil.copy(filename, filename + '.backup')
shutil.copy(filename, f'{filename}.backup')
try:
yield
except Exception:
# It failed, we need to restore from the backup.
shutil.copy(filename + '.backup', filename)
shutil.copy(f'{filename}.backup', filename)
else:
# Everything's fine, we can safely get rid of the backup.
os.remove(filename + '.backup')
os.remove(f'{filename}.backup')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function backup refactored with the following changes:



def header_link(title):
Expand All @@ -149,8 +141,6 @@ def header_link(title):
for character in title:
if character in string.whitespace:
result += '-'
elif character in string.punctuation:
pass
else:
elif character not in string.punctuation:
Comment on lines -152 to +144
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function header_link refactored with the following changes:

result += character.lower()
return result
10 changes: 4 additions & 6 deletions linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ def check(this_file, target, title, titledict):
# A directory.
if not os.path.isdir(path):
return "not a directory"
else:
# A file.
if not os.path.isfile(path):
return "not a file"
elif not os.path.isfile(path):
return "not a file"

if title is not None and title not in titledict[path]:
return "no title named %s" % title
return f"no title named {title}"
Comment on lines -70 to +74
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function check refactored with the following changes:

This removes the following comments ( why? ):

# A file.

return "ok"


Expand Down Expand Up @@ -145,7 +143,7 @@ def main():
status = check(filename, target, title, titledict)
if status != "ok":
print(" file %s, line %d: %s" % (filename, lineno, status))
print(" %s" % get_line(filename, lineno))
print(f" {get_line(filename, lineno)}")
Comment on lines -148 to +146
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

broken += 1
total += 1

Expand Down
32 changes: 13 additions & 19 deletions make-html.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

"""Create HTML files of the tutorial."""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 40-52 refactored with the following changes:


import argparse
import os
import platform
Expand All @@ -37,19 +38,15 @@
import textwrap
import webbrowser

if platform.system() == 'Windows':
python = 'py'
else:
python = 'python3'

python = 'py' if platform.system() == 'Windows' else 'python3'
try:
import mistune
except ImportError:
print("mistune isn't installed.", file=sys.stderr)
print("You can install it by running this command on a terminal or ")
print("command prompt:")
print()
print(" %s -m pip install mistune" % python)
print(f" {python} -m pip install mistune")
sys.exit(1)

try:
Expand Down Expand Up @@ -108,7 +105,7 @@ def fix_filename(filename):
# some/place/BEFORE -> some/place/AFTER
return filename[:-len(before)] + after
if filename.endswith('.md'):
filename = filename[:-3] + '.html'
filename = f'{filename[:-3]}.html'
Comment on lines -111 to +108
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function fix_filename refactored with the following changes:

return filename


Expand Down Expand Up @@ -137,7 +134,7 @@ def link(self, link, title, text):
elif '#' in link:
# it's like "some-file#title", we need to fix some-file
before, after = link.split('#', 1)
link = fix_filename(before) + '#' + after
link = f'{fix_filename(before)}#{after}'
Comment on lines -140 to +137
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TutorialRenderer.link refactored with the following changes:

else:
# it's like "some-file"
link = fix_filename(link)
Expand All @@ -164,13 +161,11 @@ def block_code(self, code, lang=None):
continue

if line.startswith('+'):
result.append('<p><font color="green">%s</font></p>'
% line.strip('+'))
result.append(f"""<p><font color="green">{line.strip('+')}</font></p>""")
elif line.startswith('-'):
result.append('<p><font color="red">%s</font></p>'
% line.strip('-'))
result.append(f"""<p><font color="red">{line.strip('-')}</font></p>""")
else:
result.append('<p>%s</p>' % line)
result.append(f'<p>{line}</p>')
Comment on lines -167 to +168
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TutorialRenderer.block_code refactored with the following changes:


return '\n'.join(result)

Expand All @@ -191,9 +186,7 @@ def table(self, header, body):

def wrap_text(text):
"""Like textwrap.fill, but respects newlines."""
result = []
for part in text.split('\n'):
result.append(textwrap.fill(part))
result = [textwrap.fill(part) for part in text.split('\n')]
Comment on lines -194 to +189
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function wrap_text refactored with the following changes:

return '\n'.join(result)


Expand Down Expand Up @@ -226,7 +219,7 @@ def main():
if pygments is None:
print("Pygments isn't installed. You can install it like this:")
print()
print(" %s -m pip install pygments" % python)
print(f" {python} -m pip install pygments")
Comment on lines -229 to +222
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

print()
print("You can also continue without Pygments, but the code examples")
print("will not be colored.")
Expand All @@ -236,8 +229,9 @@ def main():
args.pygments_style = None

if os.path.exists(args.outdir):
if not common.askyesno("%s exists. Do you want to remove it?"
% args.outdir):
if not common.askyesno(
f"{args.outdir} exists. Do you want to remove it?"
):
print("Interrupt.")
return
if os.path.isdir(args.outdir):
Expand Down
2 changes: 1 addition & 1 deletion update-readmes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def main():

# the links that point to the subdir must now point to the
# current directory, so we fix that
content = BEGINNING + content.replace(directory + '/', '').rstrip()
content = BEGINNING + content.replace(f'{directory}/', '').rstrip()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

path = os.path.join(directory, 'README.md')
this_changed = update_file(path, content)
something_changed = something_changed or this_changed
Expand Down