Skip to content

Commit

Permalink
[CodeStyle][black] use black instead of yapf as the new formatter (Pa…
Browse files Browse the repository at this point in the history
…ddlePaddle#5377)

* add black config

* blacken python code

* format a missing file
  • Loading branch information
SigureMo authored Nov 22, 2022
1 parent 53e2752 commit aed4121
Show file tree
Hide file tree
Showing 12 changed files with 601 additions and 390 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
repos:
- repo: https://github.com/pre-commit/mirrors-yapf.git
rev: v0.32.0
- repo: https://github.com/psf/black.git
rev: 22.8.0
hooks:
- id: yapf
- id: black
files: \.py$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-hooks/convert_markdown_into_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def convert_markdown_into_html(argv=None):

for filename in args.filenames:
with open(
re.sub(r"README", "index", re.sub(r"\.md$", ".html",
filename)), "w") as output:
re.sub(r"README", "index", re.sub(r"\.md$", ".html", filename)), "w"
) as output:
output.write(HEAD)
with open(filename) as input:
for line in input:
Expand Down
39 changes: 21 additions & 18 deletions ci_scripts/check_api_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ def add_path(path):

arguments = [
# flags, dest, type, default, help
[
'--rst-files', 'rst_files', str, None,
'api rst files, sperated by space'
],
['--rst-files', 'rst_files', str, None, 'api rst files, sperated by space'],
['--api-info', 'api_info_file', str, None, 'api_info_all.json filename'],
]

Expand All @@ -50,11 +47,9 @@ def parse_args():
parser = argparse.ArgumentParser(description='check api parameters')
parser.add_argument('--debug', dest='debug', action="store_true")
for item in arguments:
parser.add_argument(item[0],
dest=item[1],
help=item[4],
type=item[2],
default=item[3])
parser.add_argument(
item[0], dest=item[1], help=item[4], type=item[2], default=item[3]
)

args = parser.parse_args()
return args
Expand Down Expand Up @@ -138,13 +133,14 @@ def check_api_parameters(rstfiles, apiinfo):
"""check function's parameters same as its origin definition.
TODO:
1. All the documents of classes are skiped now. As
1. All the documents of classes are skiped now. As
(1) there ars many class methods in documents, may break the scripts.
(2) parameters of Class should be checked with its `__init__` method.
2. Some COMPLICATED annotations may break the scripts.
"""
pat = re.compile(
r'^\.\.\s+py:(method|function|class)::\s+(\S+)\s*\(\s*(.*)\s*\)\s*$')
r'^\.\.\s+py:(method|function|class)::\s+(\S+)\s*\(\s*(.*)\s*\)\s*$'
)
check_passed = []
check_failed = []
api_notfound = []
Expand All @@ -166,35 +162,41 @@ def check_api_parameters(rstfiles, apiinfo):
flag = False
func_found_in_json = False
for apiobj in apiinfo.values():
if 'all_names' in apiobj and funcname in apiobj[
'all_names']:
if (
'all_names' in apiobj
and funcname in apiobj['all_names']
):
func_found_in_json = True
if 'args' in apiobj:
if paramstr == apiobj['args']:
print(
f'check func:{funcname} in {rstfilename} with {paramstr}'
)
flag = _check_params_in_description(
rstfilename, paramstr)
rstfilename, paramstr
)
else:
print(
f'check func:{funcname} in {rstfilename} with {paramstr}, but different with json\'s {apiobj["args"]}'
)
flag = _check_params_in_description(
rstfilename, paramstr)
rstfilename, paramstr
)
else: # paddle.abs class_method does not have `args` in its json item.
print(
f'check func:{funcname} in {rstfilename} with its FullArgSpec'
)
flag = _check_params_in_description_with_fullargspec(
rstfilename, funcname)
rstfilename, funcname
)
break
if not func_found_in_json: # may be inner functions
print(
f'check func:{funcname} in {rstfilename} with its FullArgSpec'
)
flag = _check_params_in_description_with_fullargspec(
rstfilename, funcname)
rstfilename, funcname
)
if flag:
check_passed.append(rstfile)
print(f'check success: {rstfile}')
Expand All @@ -214,7 +216,8 @@ def check_api_parameters(rstfiles, apiinfo):
rstfiles = [fn for fn in args.rst_files.split(' ') if fn]
apiinfo = json.load(open(args.api_info_file))
check_passed, check_failed, api_notfound = check_api_parameters(
rstfiles=rstfiles, apiinfo=apiinfo)
rstfiles=rstfiles, apiinfo=apiinfo
)
result = True
if check_failed:
result = False
Expand Down
40 changes: 25 additions & 15 deletions ci_scripts/chinese_samplecode_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def extract_sample_code(srcfile, status_all):
srcfile.seek(0, 0)
srcls = srcfile.readlines()
srcls = remove_desc_code(
srcls, filename) # remove description info for samplecode
srcls, filename
) # remove description info for samplecode
status = []
sample_code_begins = find_all(srcc, " code-block:: python")
if len(sample_code_begins) == 0:
Expand All @@ -108,18 +109,22 @@ def extract_sample_code(srcfile, status_all):
startindent = ""
# remove indent error
if srcls[start + blank_line].find("from") != -1:
startindent += srcls[start + blank_line][:srcls[
start + blank_line].find("from")]
startindent += srcls[start + blank_line][
: srcls[start + blank_line].find("from")
]
elif srcls[start + blank_line].find("import") != -1:
startindent += srcls[start + blank_line][:srcls[
start + blank_line].find("import")]
startindent += srcls[start + blank_line][
: srcls[start + blank_line].find("import")
]
else:
startindent += check_indent(srcls[start + blank_line])
content += srcls[start + blank_line][len(startindent):]
content += srcls[start + blank_line][len(startindent) :]
for j in range(start + blank_line + 1, len(srcls)):
# planish a blank line
if not srcls[j].startswith(startindent) and srcls[
j] != '\n':
if (
not srcls[j].startswith(startindent)
and srcls[j] != '\n'
):
break
if srcls[j].find(" code-block:: python") != -1:
break
Expand All @@ -132,16 +137,18 @@ def extract_sample_code(srcfile, status_all):

def run_sample_code(content, filename):
# three status ,-1:no sample code; 1: running error; 0:normal
fname = filename.split("/")[-1].replace("_cn", "").replace(".rst",
"") + ".py"
fname = (
filename.split("/")[-1].replace("_cn", "").replace(".rst", "") + ".py"
)
tempf = open("temp/" + fname, 'w')
content = "# -*- coding: utf-8 -*-\n" + content
tempf.write(content)
tempf.close()
cmd = ["python", "temp/" + fname]

subprc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
_, error = subprc.communicate()
err = "".join(error.decode(encoding='utf-8'))

Expand Down Expand Up @@ -221,12 +228,15 @@ def test(file):
type_two_number = len(status_groups[1])
if type_two_number > 0:
print("Error type two sample number is:{}".format(type_two_number))
print("Error raised from type two:running error sample code.",
str(status_groups[1]))
print(
"Error raised from type two:running error sample code.",
str(status_groups[1]),
)
if type_one_number > 0:
print("Error type one sample number is:{}".format(type_one_number))
print("Error raised from type one:no sample code.",
str(status_groups[-1]))
print(
"Error raised from type one:no sample code.", str(status_groups[-1])
)
# no sample code not considered as error now.
if type_two_number == 0:
ci_pass = True
Expand Down
95 changes: 51 additions & 44 deletions ci_scripts/doc-build-config/en/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os, subprocess

# sys.path.insert(0, os.path.abspath('@PADDLE_BINARY_DIR@/python'))
import shlex
from recommonmark import parser, transform
Expand Down Expand Up @@ -57,33 +58,26 @@

exhale_args = {
# These arguments are required
"containmentFolder":
"/FluidDoc/docs/inference_api",
"rootFileName":
"library_root.rst",
"rootFileTitle":
"Inference API",
"doxygenStripFromPath":
"..",
#"listingExclude": [r"*CMakeLists*", 0],
"containmentFolder": "/FluidDoc/docs/inference_api",
"rootFileName": "library_root.rst",
"rootFileTitle": "Inference API",
"doxygenStripFromPath": "..",
# "listingExclude": [r"*CMakeLists*", 0],
# Suggested optional arguments
"createTreeView":
True,
"createTreeView": True,
# TIP: if using the sphinx-bootstrap-theme, you need
# "treeViewIsBootstrap": True,
"exhaleExecutesDoxygen":
True,
"exhaleDoxygenStdin":
"INPUT=/FluidDoc/docs/inference_api/\nMACRO_EXPANSION=NO\nSKIP_FUNCTION_MACROS=YES",
"verboseBuild":
True,
"generateBreatheFileDirectives":
True
"exhaleExecutesDoxygen": True,
"exhaleDoxygenStdin": "INPUT=/FluidDoc/docs/inference_api/\nMACRO_EXPANSION=NO\nSKIP_FUNCTION_MACROS=YES",
"verboseBuild": True,
"generateBreatheFileDirectives": True,
}

MARKDOWN_EXTENSIONS = [
'markdown.extensions.fenced_code', 'markdown.extensions.tables',
'pymdownx.superfences', 'pymdownx.escapeall'
'markdown.extensions.fenced_code',
'markdown.extensions.tables',
'pymdownx.superfences',
'pymdownx.escapeall',
]

html_baseurl = 'https://www.paddlepaddle.org.cn/documentation/docs/'
Expand Down Expand Up @@ -124,8 +118,14 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = [
'_build', '**/*_cn*', 'book/*', 'design/*', '*_cn.rst', '**/*.cn*',
'*.cn*', '**/*hidden.*'
'_build',
'**/*_cn*',
'book/*',
'design/*',
'*_cn.rst',
'**/*.cn*',
'*.cn*',
'**/*hidden.*',
]

# The reST default role (used for this markup: `text`) to use for all
Expand Down Expand Up @@ -169,8 +169,11 @@
float(os.environ['VERSIONSTR'])
html_context['github_version'] = 'release/' + os.environ['VERSIONSTR']
except ValueError:
print("os.environ['VERSIONSTR']={} is not releases's name".format(
os.environ['VERSIONSTR']))
print(
"os.environ['VERSIONSTR']={} is not releases's name".format(
os.environ['VERSIONSTR']
)
)
html_context['github_version'] = os.environ['VERSIONSTR']

# if lang == 'en' and 'pagename' in html_context and html_context['pagename'].startswith('api/'):
Expand All @@ -193,7 +196,7 @@
'sticky_navigation': True,
'navigation_depth': 10,
'includehidden': True,
'titles_only': True
'titles_only': True,
}

# Add any paths that contain custom static files (such as style sheets) here,
Expand All @@ -210,17 +213,14 @@
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
'fncychap': '',

# Additional stuff for the LaTeX preamble.
#
'preamble': r'''\usepackage{ctex}
''',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
Expand Down Expand Up @@ -276,37 +276,42 @@ def linkcode_resolve(domain, info):
if type(api).__name__ == 'module':
module = os.path.splitext(api.__file__)[0] + '.py'
else:
node_definition = ast.ClassDef if inspect.isclass(
api) else ast.FunctionDef
node_definition = (
ast.ClassDef if inspect.isclass(api) else ast.FunctionDef
)

if api.__module__ not in [
'paddle.fluid.core',
'paddle.fluid.layers.layer_function_generator'
'paddle.fluid.core',
'paddle.fluid.layers.layer_function_generator',
]:
module = os.path.splitext(
sys.modules[api.__module__].__file__)[0] + '.py'
module = (
os.path.splitext(sys.modules[api.__module__].__file__)[0]
+ '.py'
)
with open(module) as module_file:
module_ast = ast.parse(module_file.read())

for node in module_ast.body:
if isinstance(
node,
node_definition) and node.name == api_title:
if (
isinstance(node, node_definition)
and node.name == api_title
):
line_no = node.lineno
break

# If we could not find it, we look at assigned objects.
if not line_no:
for node in module_ast.body:
if isinstance(node, ast.Assign) and api_title in [
target.id for target in node.targets
target.id for target in node.targets
]:
line_no = node.lineno
break
else:
module = os.path.splitext(current_class.__file__)[0] + '.py'
url = GITHUB_REPO_URL + os.path.join(doc_version, 'python',
module[module.rfind('paddle'):])
url = GITHUB_REPO_URL + os.path.join(
doc_version, 'python', module[module.rfind('paddle') :]
)
if line_no:
return url + '#L' + str(line_no)
return url
Expand All @@ -333,7 +338,8 @@ def handle_api_aliases():
if language in config:
for target_name, origin_name in config[language].items():
print(
f'conf.py(en) handle_api_aliases: {target_name}={origin_name}')
f'conf.py(en) handle_api_aliases: {target_name}={origin_name}'
)
tname = target_name.strip()
tmn, tn = tname.rsplit('.', 1)
oname = origin_name.strip()
Expand All @@ -359,7 +365,8 @@ def setup(app):
'enable_eval_rst': True,
# 'enable_auto_doc_ref': True,
'auto_toc_tree_section': True,
'known_url_schemes': ['http', 'https']
'known_url_schemes': ['http', 'https'],
},
True)
True,
)
app.add_transform(AutoStructify)
Loading

0 comments on commit aed4121

Please sign in to comment.