Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 4fb26f5

Browse files
Merge branch 'template_refactoring'
2 parents 65ed25c + f4133f0 commit 4fb26f5

File tree

16 files changed

+1460
-1186
lines changed

16 files changed

+1460
-1186
lines changed

setup.cfg

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[tool:pytest]
2+
minversion = 6.0
3+
addopts = -ra -q -s
4+
testpaths =
5+
tests
6+
7+
[yapf]
8+
based_on_style = google
9+
10+
# The number of columns to use for indentation.
11+
indent_width = 4
12+
13+
# The column limit.
14+
column_limit = 80
15+
16+
# Place each dictionary entry onto its own line.
17+
each_dict_entry_on_separate_line = True
18+
19+
# Put closing brackets on a separate line, dedented, if the bracketed
20+
# expression can't fit in a single line. Applies to all kinds of brackets,
21+
# including function definitions and calls. For example:
22+
#
23+
# config = {
24+
# 'key1': 'value1',
25+
# 'key2': 'value2',
26+
# } # <--- this bracket is dedented and on a separate line
27+
#
28+
# time_series = self.remote_client.query_entity_counters(
29+
# entity='dev3246.region1',
30+
# key='dns.query_latency_tcp',
31+
# transform=Transformation.AVERAGE(window=timedelta(seconds=60)),
32+
# start_ts=now()-timedelta(days=3),
33+
# end_ts=now(),
34+
# ) # <--- this bracket is dedented and on a separate line
35+
dedent_closing_brackets = True
36+
37+
# Do not split consecutive brackets. Only relevant when DEDENT_CLOSING_BRACKETS is set
38+
coalesce_brackets = True
39+
40+
# Align closing bracket with visual indentation.
41+
align_closing_bracket_with_visual_indent = False
42+
43+
# Split named assignments onto individual lines.
44+
split_before_named_assigns = True
45+
46+
# If an argument / parameter list is going to be split, then split before the first argument.
47+
split_before_first_argument = True
48+
49+
# Allow splitting before a default / named assignment in an argument list.
50+
allow_split_before_default_or_named_assigns = True
51+
52+
# Join short lines into one line. E.g., single line if statements.
53+
join_multiple_lines = False
54+
55+
# Let spacing indicate operator precedence.
56+
arithmetic_precedence_indication = True
57+
58+
# Do not include spaces around selected binary operators.
59+
# Example: 1 + 2 * 3 - 4 / 5 => 1 + 2*3 - 4/5
60+
no_spaces_around_selected_binary_operators = True
61+
62+
# Allow lambdas to be formatted on more than one line.
63+
allow_multiline_lambdas = True

tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-

tests/test_yapf_format.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
import unittest
6+
7+
import pygments
8+
from pygments import console
9+
10+
from tests.utils import list_all_py_files
11+
from tests.utils import CustomTestCase
12+
13+
from yapf.yapflib.yapf_api import FormatCode
14+
15+
16+
def _read_utf_8_file(filename):
17+
if sys.version_info.major == 2: ## Python 2 specific
18+
with open(filename, 'rb') as f:
19+
return unicode(f.read(), 'utf-8')
20+
else:
21+
with open(filename, encoding='utf-8') as f:
22+
return f.read()
23+
24+
25+
def print_color(msg, color):
26+
print(pygments.console.colorize(color, msg))
27+
28+
29+
class YAPF_Style_Test(unittest.TestCase):
30+
31+
@classmethod
32+
def setUpClass(cls):
33+
34+
cls.badly_formatted_files = list()
35+
cls.files_2_test = list_all_py_files()
36+
37+
def test_files_format(self):
38+
39+
total_analyzed_files = 0
40+
for file in list_all_py_files():
41+
42+
total_analyzed_files += 1
43+
44+
try:
45+
46+
print(f"Testing: {file:100s}", end="")
47+
code = _read_utf_8_file(file)
48+
49+
# https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module
50+
diff, changed = FormatCode(
51+
code,
52+
filename=file,
53+
style_config='setup.cfg',
54+
print_diff=True
55+
)
56+
57+
if changed:
58+
print_color("FAILURE", "red")
59+
self.badly_formatted_files.append(file)
60+
else:
61+
print_color("SUCCESS", "green")
62+
63+
except Exception as e:
64+
print_color("FAILURE", "red")("FAILURE")
65+
print(
66+
"Error while processing file: `%s`\n"
67+
"Error: %s" % (file, str(e))
68+
)
69+
70+
str_err = ""
71+
72+
if self.badly_formatted_files:
73+
for filename in self.badly_formatted_files:
74+
str_err += f"yapf -i --style=setup.cfg {filename}\n"
75+
76+
str_err = "\n======================================================================================\n" \
77+
f"Bad Coding Style: {len(self.badly_formatted_files)} file(s) need to be formatted, run the following commands to fix: \n" \
78+
f"{str_err}" \
79+
"======================================================================================"
80+
81+
passing_files = total_analyzed_files - len(self.badly_formatted_files)
82+
print_color(
83+
f"\nPASSING: {passing_files} / {total_analyzed_files}",
84+
"green" if str_err == "" else "red"
85+
)
86+
87+
if str_err != "":
88+
print_color(str_err, "red")
89+
90+
self.assertEqual(str_err, "")
91+
92+
93+
if __name__ == '__main__':
94+
unittest.main()

tests/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import os
5+
import unittest
6+
7+
from contextlib import contextmanager
8+
from glob import glob, iglob
9+
10+
__all__ = [
11+
'CustomTestCase',
12+
'list_all_py_files',
13+
]
14+
15+
16+
class CustomTestCase(unittest.TestCase):
17+
18+
@contextmanager
19+
def assertNotRaises(self, exc_type):
20+
try:
21+
yield None
22+
except exc_type:
23+
raise self.failureException('{} raised'.format(exc_type.__name__))
24+
25+
26+
_excludes_paths = ["tftrt/blog_posts/", "tftrt/examples/third_party"]
27+
28+
29+
def list_all_py_files():
30+
for _dir in ['tests', 'tftrt']:
31+
for _file in iglob(f"{_dir}/**/*.py", recursive=True):
32+
if any([path in _file for path in _excludes_paths]):
33+
continue
34+
yield _file

0 commit comments

Comments
 (0)