summaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
authorAlberto Bertogli <albertito@blitiri.com.ar>2020-05-24 15:20:56 +0200
committerAlberto Bertogli <albertito@blitiri.com.ar>2020-05-24 17:04:24 +0200
commit20b99ee568fd15ca2897f995e9775861ef293544 (patch)
tree4d1ad17d066f2e05a7927245136b78188d32f79e /utils.py
parentad950208bf19d49e8f825007ba267ca3c43b8390 (diff)
downloadgit-arr-fork-20b99ee568fd15ca2897f995e9775861ef293544.zip
Introduce type annotations
This patch introduces type annotations, which can be checked with mypy. The coverage is not very comprehensive for now, but it is a starting point and will be expanded in later patches.
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py42
1 files changed, 22 insertions, 20 deletions
diff --git a/utils.py b/utils.py
index 0f25be1..9c2c45a 100644
--- a/utils.py
+++ b/utils.py
@@ -5,16 +5,16 @@ These are mostly used in templates, for presentation purposes.
"""
try:
- import pygments
- from pygments import highlight
- from pygments import lexers
- from pygments.formatters import HtmlFormatter
+ import pygments # type: ignore
+ from pygments import highlight # type: ignore
+ from pygments import lexers # type: ignore
+ from pygments.formatters import HtmlFormatter # type: ignore
except ImportError:
pygments = None
try:
- import markdown
- import markdown.treeprocessors
+ import markdown # type: ignore
+ import markdown.treeprocessors # type: ignore
except ImportError:
markdown = None
@@ -23,14 +23,16 @@ import mimetypes
import string
import os.path
+import git
-def shorten(s, width=60):
+
+def shorten(s: str, width=60):
if len(s) < 60:
return s
return s[:57] + "..."
-def can_colorize(s):
+def can_colorize(s: str):
"""True if we can colorize the string, False otherwise."""
if pygments is None:
return False
@@ -54,7 +56,7 @@ def can_colorize(s):
return True
-def can_markdown(repo, fname):
+def can_markdown(repo: git.Repo, fname: str):
"""True if we can process file through markdown, False otherwise."""
if markdown is None:
return False
@@ -75,14 +77,14 @@ def can_embed_image(repo, fname):
)
-def colorize_diff(s):
+def colorize_diff(s: str) -> str:
lexer = lexers.DiffLexer(encoding="utf-8")
formatter = HtmlFormatter(encoding="utf-8", cssclass="source_code")
return highlight(s, lexer, formatter)
-def colorize_blob(fname, s):
+def colorize_blob(fname, s: str) -> str:
try:
lexer = lexers.guess_lexer_for_filename(fname, s, encoding="utf-8")
except lexers.ClassNotFound:
@@ -107,7 +109,7 @@ def colorize_blob(fname, s):
return highlight(s, lexer, formatter)
-def markdown_blob(s):
+def markdown_blob(s: str) -> str:
extensions = [
"markdown.extensions.fenced_code",
"markdown.extensions.tables",
@@ -116,7 +118,7 @@ def markdown_blob(s):
return markdown.markdown(s, extensions=extensions)
-def embed_image_blob(fname, image_data):
+def embed_image_blob(fname: str, image_data: bytes) -> str:
mimetype = mimetypes.guess_type(fname)[0]
b64img = base64.b64encode(image_data).decode("ascii")
return '<img style="max-width:100%;" src="data:{0};base64,{1}" />'.format(
@@ -124,22 +126,22 @@ def embed_image_blob(fname, image_data):
)
-def is_binary(s):
+def is_binary(b: bytes):
# Git considers a blob binary if NUL in first ~8KB, so do the same.
- return b"\0" in s[:8192]
+ return b"\0" in b[:8192]
-def hexdump(s):
+def hexdump(s: bytes):
graph = string.ascii_letters + string.digits + string.punctuation + " "
- s = s.decode("latin1")
+ b = s.decode("latin1")
offset = 0
- while s:
- t = s[:16]
+ while b:
+ t = b[:16]
hexvals = ["%.2x" % ord(c) for c in t]
text = "".join(c if c in graph else "." for c in t)
yield offset, " ".join(hexvals[:8]), " ".join(hexvals[8:]), text
offset += 16
- s = s[16:]
+ b = b[16:]
if markdown: