Skip to content

Commit ffb4e9d

Browse files
authored
Merge pull request #654 from airbnb/csharplus-improve-code-quality
Refactoring to improve code quality
2 parents 690f245 + 1f6da0b commit ffb4e9d

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

knowledge_repo/app/routes/debug.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def show_views():
8686

8787
methods = ','.join(rule.methods)
8888
url = url_for(rule.endpoint, **options)
89-
line = unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
89+
line = unquote(f'{rule.endpoint:50s} {methods:20s} {url}')
9090
output.append(line)
9191

9292
return "<br />".join(sorted(output))

knowledge_repo/app/utils/time.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def human_readable_time_delta(seconds):
1515
if seconds is None:
1616
return "Never"
1717
elif seconds < 60:
18-
return "{:d} seconds ago".format(int(round(seconds)))
18+
return f'{seconds:.0f} seconds ago'
1919
elif seconds < 60 * 60:
20-
return "{:d} minutes ago".format(int(round(seconds / 60)))
20+
return f'{(seconds / 60):.0f} minutes ago'
2121
elif seconds < 24 * 60 * 60:
22-
return "{:d} hours ago".format(int(round(seconds / 60 / 60)))
22+
return f'{(seconds / 60 / 60):.0f} hours ago'
2323
else:
24-
return "{:d} days ago".format(int(round(seconds / 60 / 60 / 24)))
24+
return f'{(seconds / 60 / 60 / 24):.0f} days ago'

knowledge_repo/converters/html.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ def handleMatch(self, m):
188188

189189
class MathJaxExtension(markdown.Extension):
190190
def extendMarkdown(self, md, md_globals=None):
191-
# Needs to come before escape matching because \ is pretty important in LaTeX
191+
# Needs to come before escape matching because \ is
192+
# pretty important in LaTeX
192193
md.inlinePatterns.add('mathjax', MathJaxPattern(), '<escape')
193194

194195

@@ -217,7 +218,10 @@ def _render_markdown(self,
217218

218219
# proxy posts are assumed to be embeddable links
219220
if PROXY in self.kp.headers:
220-
return None, '<a href="{0}">Linked Post</a>\n<iframe width=100% height=1000 src="{0}"></iframe>'.format(self.kp.headers['proxy'].strip())
221+
proxy = self.kp.headers[PROXY].strip()
222+
return None, (f'<a href="{proxy}">Linked Post</a>\n'
223+
f'<iframe width=100% height=1000 '
224+
f'src="{proxy}"></iframe>')
221225

222226
html = ''
223227
if not skip_headers:
@@ -256,7 +260,8 @@ def urlmapper_proxy(name, match):
256260
return match.group(0).replace(match.group('url'), new_url)
257261
return None
258262

259-
return SubstitutionMapper(patterns=patterns, mappers=[urlmapper_proxy]).apply(html)
263+
return SubstitutionMapper(patterns=patterns,
264+
mappers=[urlmapper_proxy]).apply(html)
260265

261266
# Utility methods
262267
def render_headers(self):
@@ -287,5 +292,6 @@ def base64_encode_image_mapper(self, tag, url):
287292
image_data = base64.b64encode(self.kp_images[url])
288293
image_mimetype = mimetypes.guess_type(url)[0]
289294
if image_mimetype is not None:
290-
return f'data:{image_mimetype};base64, ' + image_data.decode(UTF8)
295+
return f'data:{image_mimetype};base64, ' + \
296+
image_data.decode(UTF8)
291297
return None

knowledge_repo/postprocessors/extract_images.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def find_images(self, md):
6565

6666
def collect_images_for_pattern(self, md, pattern=None):
6767
p = re.compile(pattern)
68-
return [{'offset': m.start(), 'tag': m.group(0), 'src': m.group('src')} for m in p.finditer(md)]
68+
return [{'offset': m.start(), 'tag': m.group(0), 'src': m.group('src')}
69+
for m in p.finditer(md)]
6970

7071
def collect_images(self, kp, images):
7172
image_mapping = {}
@@ -99,7 +100,8 @@ def collect_images(self, kp, images):
99100
def skip_image(self, kp, image):
100101
if re.match('http[s]?://', image['src']):
101102
return True
102-
if image['src'].startswith('images/') and image['src'] in kp.image_paths:
103+
if image['src'].startswith('images/') and \
104+
image['src'] in kp.image_paths:
103105
return True
104106
return False
105107

knowledge_repo/postprocessors/extract_images_to_local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def copy_image(self, kp, img_path, is_ref=False, repo_name='knowledge'):
5656

5757
# Copy images to local http server directory
5858
new_path = os.path.join(self.image_dir, fname_img)
59-
logger.info("Copying image {} to {}".format(tmp_path, new_path))
59+
logger.info(f'Copying image {tmp_path} to {new_path}')
6060
try:
6161
shutil.copyfile(tmp_path, new_path)
6262
except Exception as e:

knowledge_repo/postprocessors/extract_images_to_s3.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ def copy_image(self, kp, img_path, is_ref=False, repo_name='knowledge'):
4545
img_ext = posixpath.splitext(img_path)[1]
4646

4747
# Make random filename for image
48-
random_string = ''.join(random.choice(
49-
string.ascii_lowercase) for i in range(6))
50-
fname_img = '{repo_name}_{time}_{random_string}{ext}'.format(
51-
repo_name=repo_name,
52-
time=int(round(time.time() * 100)),
53-
random_string=random_string,
54-
ext=img_ext).strip().replace(' ', '-')
48+
random_name = ''.join(
49+
random.choice(string.ascii_lowercase) for i in range(6))
50+
timestamp = int(round(time.time() * 100))
51+
fname_img = (f'{repo_name}_{timestamp}_'
52+
f'{random_name}{img_ext}').strip().replace(' ', '-')
5553

5654
# Copy image to accessible folder on S3
5755
fname_s3 = posixpath.join(self.s3_image_root, repo_name, fname_img)

0 commit comments

Comments
 (0)