Skip to content

Commit

Permalink
tooling: add black and flake8 for python formatting/linting (#6454)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin authored Sep 3, 2021
1 parent f4390b1 commit 3fa29ea
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 125 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
max-line-length = 88
# We ignore E501 as black handles it for us, and in a way that ignores strings
# that go over the line length, as opposed to flake8 which flags such strings.
extend-ignore = E203,E501
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ jobs:
name: CI

steps:
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Checkout
uses: actions/checkout@v2
Expand All @@ -22,6 +26,9 @@ jobs:
- name: Install npm dependencies
run: npm ci

- name: Install python dependencies
run: python3 -m pip install -r requirements.txt

- name: Test
run: npm test

Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
black==21.8b0
flake8==3.9.2
51 changes: 36 additions & 15 deletions scripts/pdf/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import argparse
from datetime import datetime

from weasyprint import HTML, CSS
from weasyprint import HTML


def main(loc, colorscheme):

Expand All @@ -30,45 +31,65 @@ def main(loc, colorscheme):
csslist.append(colorscheme + ".css")

# A string that stores all pages in HTML format
html = '<!doctype html><html><head><meta charset="utf-8"></head>' \
+"<body><h1 class=title-main>tldr pages</h1>" \
+ "<h4 class=title-sub>Simplified and community-driven man pages</h4>" \
+ "<h6 class=title-sub><em><small>Generated on " + datetime.now().strftime("%c") + "</small></em></h6>" \
html = (
'<!doctype html><html><head><meta charset="utf-8"></head>'
+ "<body><h1 class=title-main>tldr pages</h1>"
+ "<h4 class=title-sub>Simplified and community-driven man pages</h4>"
+ "<h6 class=title-sub><em><small>Generated on "
+ datetime.now().strftime("%c")
+ "</small></em></h6>"
+ '<p style="page-break-before: always" ></p>'
)

# Writing names of all directories inside 'pages' to a list
for operating_sys in sorted(os.listdir(loc)):

# Required string to create directory title pages
html += "<h2 class=title-dir>" + operating_sys.capitalize() + "</h2>" \
html += (
"<h2 class=title-dir>"
+ operating_sys.capitalize()
+ "</h2>"
+ '<p style="page-break-before: always" ></p>'
)

# Conversion of Markdown to HTML string
for page_number, md in enumerate(sorted(glob.glob(os.path.join(loc, operating_sys, "*.md"))), start=1):
for page_number, md in enumerate(
sorted(glob.glob(os.path.join(loc, operating_sys, "*.md"))), start=1
):
with open(md, "r") as inp:
text = inp.readlines()
for line in text:
if re.match(r'^>', line):
line = line[:0] + '####' + line[1:]
if re.match(r"^>", line):
line = line[:0] + "####" + line[1:]
html += markdown.markdown(line)
html += '<p style="page-break-before: always" ></p>'
print(f"Rendered page {page_number} of the directory {operating_sys}")

html += "</body></html>"

# Writing the PDF to disk
print("\nConverting all pages to PDF...")
HTML(string=html).write_pdf("tldr-pages.pdf", stylesheets=csslist)

if os.path.exists("tldr-pages.pdf"):
print("\nCreated tldr-pages.pdf in the current directory!\n")


if __name__ == "__main__":

# Parsing the arguments
parser = argparse.ArgumentParser(prog="tldr-pages-to-pdf", description="A Python script to generate a single PDF document with all the `tldr` pages.")
parser.add_argument("dir_path", help = "Path to the 'pages' directory")
parser.add_argument("-c", "--color", choices=["solarized-light", "solarized-dark", "basic"], default="basic", help="Color scheme of the PDF")
parser = argparse.ArgumentParser(
prog="tldr-pages-to-pdf",
description="A Python script to generate a single PDF document with all the `tldr` pages.",
)
parser.add_argument("dir_path", help="Path to the 'pages' directory")
parser.add_argument(
"-c",
"--color",
choices=["solarized-light", "solarized-dark", "basic"],
default="basic",
help="Color scheme of the PDF",
)
args = parser.parse_args()

main(args.dir_path, args.color)
101 changes: 54 additions & 47 deletions scripts/send-to-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import sys
import requests

BOT_URL = 'https://tldr-bot.starbeamrainbowlabs.com'
BOT_URL = "https://tldr-bot.starbeamrainbowlabs.com"

COMMENT_ERROR="""
COMMENT_ERROR = """
The [build](https://github.com/tldr-pages/tldr/actions/runs/{build_id}) for this PR failed with the following error(s):
```
Expand All @@ -17,7 +17,7 @@
Please fix the error(s) and push again.
"""

COMMENT_CHECK="""
COMMENT_CHECK = """
Hello! I've noticed something unusual when checking this PR:
{content}
Expand All @@ -27,62 +27,69 @@

################################################################################


def post_comment(pr_id, body, once):
endpoint = BOT_URL + '/comment'
endpoint = BOT_URL + "/comment"

if once:
endpoint += "/once"

data = {"pr_id": pr_id, "body": body}

try:
with requests.post(endpoint, json=data) as r:
if r.status_code != requests.codes.ok:
print(
"Error: tldr-bot responded with code",
r.status_code,
file=sys.stderr,
)
print(r.text, file=sys.stderr)
return False
except requests.exceptions.RequestException as e:
print("Error sending data to tldr-bot:", str(e), file=sys.stderr)
return False

if once:
endpoint += '/once'
return True

data = {'pr_id': pr_id, 'body': body}

try:
with requests.post(endpoint, json=data) as r:
if r.status_code != requests.codes.ok:
print('Error: tldr-bot responded with code', r.status_code, file=sys.stderr)
print(r.text, file=sys.stderr)
return False
except requests.exceptions.RequestException as e:
print('Error sending data to tldr-bot:', str(e), file=sys.stderr)
return False

return True

def main(action):
if action not in ('report-errors', 'report-check-results'):
print('Unknown action:', action, file=sys.stderr)
sys.exit(1)
if action not in ("report-errors", "report-check-results"):
print("Unknown action:", action, file=sys.stderr)
sys.exit(1)

content = sys.stdin.read().strip()

content = sys.stdin.read().strip()
if action == "report-errors":
comment_body = COMMENT_ERROR.format(build_id=BUILD_ID, content=content)
comment_once = False
elif action == "report-check-results":
comment_body = COMMENT_CHECK.format(content=content)
comment_once = True

if action == 'report-errors':
comment_body = COMMENT_ERROR.format(build_id=BUILD_ID, content=content)
comment_once = False
elif action == 'report-check-results':
comment_body = COMMENT_CHECK.format(content=content)
comment_once = True
if post_comment(PR_ID, comment_body, comment_once):
print("Success.")
else:
print("Error sending data to tldr-bot!", file=sys.stderr)

if post_comment(PR_ID, comment_body, comment_once):
print('Success.')
else:
print('Error sending data to tldr-bot!', file=sys.stderr)

################################################################################

if __name__ == '__main__':
REPO_SLUG = os.environ.get('GITHUB_REPOSITORY')
PR_ID = os.environ.get('PULL_REQUEST_ID')
BUILD_ID = os.environ.get('GITHUB_RUN_ID')
if __name__ == "__main__":
REPO_SLUG = os.environ.get("GITHUB_REPOSITORY")
PR_ID = os.environ.get("PULL_REQUEST_ID")
BUILD_ID = os.environ.get("GITHUB_RUN_ID")

if PR_ID is None or BUILD_ID is None or REPO_SLUG is None:
print('Needed environment variables are not set.', file=sys.stderr)
sys.exit(1)
if PR_ID is None or BUILD_ID is None or REPO_SLUG is None:
print("Needed environment variables are not set.", file=sys.stderr)
sys.exit(1)

if PR_ID is None or PR_ID == 'false':
print('Not a pull request, refusing to run.', file=sys.stderr)
sys.exit(0)
if PR_ID is None or PR_ID == "false":
print("Not a pull request, refusing to run.", file=sys.stderr)
sys.exit(0)

if len(sys.argv) != 2:
print('Usage:', sys.argv[0], '<ACTION>', file=sys.stderr)
sys.exit(1)
if len(sys.argv) != 2:
print("Usage:", sys.argv[0], "<ACTION>", file=sys.stderr)
sys.exit(1)

main(sys.argv[1])
main(sys.argv[1])
Loading

0 comments on commit 3fa29ea

Please sign in to comment.