def _black(file_name): import black path = os.path.join(dir_path, file_name) with open(path, "r", encoding='utf8') as f: contents = f.read() with open(path + '.exp', "r", encoding='utf8') as f: expected_contents = f.read() # Run black on the file. We manually encode and decode ourselves # to avoid needing to make black find the right codec. with tempfile.TemporaryDirectory() as tmp_dir: tmp_path = os.path.join(tmp_dir, file_name) with open(tmp_path, "w", encoding='utf8') as f: f.write(pyxl_transform_string(contents, invertible=True)) try: black.main([tmp_path]) except SystemExit as e: pass with open(tmp_path, "r", encoding='utf8') as f: actual_contents = pyxl_invert_string(f.read()) assert expected_contents == actual_contents, ( "Black output did not match expected for file %s" % file_name)
def render_py_file(template_path: Path, output: Path, context: Dict[str, Any]) -> None: import black render_file(template_path, output, context) try: black.main([str(output), "-q"]) except SystemExit: pass
def _do_action(cls, parsed_args): import black sys.argv = ['black', parsed_args.filename, '-l', 80] try: black.main() except SystemExit: # black calls quit(), ignore it pass
def black_wrapper(writeback): """Helper function to invoke black programatically.""" check = [] if writeback else ["--check"] sys.argv[1:] = check + ROOT_DIR import black black.main()
def black_wrapper(writeback): """Helper function to invoke black programatically.""" check = [] if writeback else ["--check"] exclude = "|".join(["cangivefilenameshere"]) sys.argv[1:] = check + ["--exclude", exclude, ROOT_DIR] import black black.main()
def black_wrapper(writeback): """ Helper function to invoke black programatically. """ check = [] if writeback else ["--check"] exclude = "|".join(["_tifffile\.py"]) sys.argv[1:] = check + ["--exclude", exclude, ROOT_DIR] import black black.main()
def main(*args, **kwargs) -> None: # set_fork_method() mp.freeze_support() try: # monkey patch black.format_str = format_str black.main.help = "The aligning code formatter." black.main = click.version_option(version=__version__)(black.main) black.main(*args, **kwargs) finally: # monkey unpatch black.format_str = original_format_str
def format(file_or_dir, *additional_formatter_args): """Format the specified file or directory using the builtin config.""" try: black.main( [ str(file_or_dir), f"--config={_config_constants.BLACK_CONFIG_FILE.resolve()}", *additional_formatter_args, ] ) except SystemExit as e: if e.code == 0: pass # why are they exiting when called via import ?! ‾\_(ツ)_/‾ else: raise
def main(): func_name = 'normalize_string_quotes' old_text = """ orig_quote == '"': """.strip() new_text = """ orig_quote == "'": """.strip() exec(_get_func_def(func_name).replace(old_text, new_text)) black.normalize_string_quotes = vars()[func_name] return black.main()
def main(): config_file_opt = [p for p in black.main.params if p.name == 'config'][0] config_file_opt.callback = read_config_file options = [p for p in black.main.params if p.name != 'config'] options.append(config_file_opt) black.main.params = options return black.main()
def format_lines(lines: List[str], black_args=None) -> List[str]: if black_args is None: black_args = [] with tempfile.NamedTemporaryFile(suffix=".py", dir=os.getcwd(), mode="wt+", delete=True) as fp: # Copy the input. fp.writelines(lines) fp.flush() # Run black. if "--quiet" not in black_args: black_args.append("--quiet") black_args.append(fp.name) try: exit_code = black.main(args=black_args) except SystemExit as exc: exit_code = exc.code if exit_code == 0: # Write output. fp.seek(0) return cast(List[str], fp.readlines()) raise RuntimeError("black failed", exit_code)
def lint_python(genny_repo_root: str, fix: bool = False): path = os.path.join(genny_repo_root, "src", "lamplib") if not fix: cmd = ["--check"] else: cmd = [] cmd.append(path) SLOG.debug("Running black", black_args=cmd) try: black.main(cmd) except SystemExit as e: if e.code == 0: return msg = "There were python formatting errors." if not fix: msg += " Run the command with the --fix option to fix." else: msg += " Some errors may have been fixed. Re-run to verify." SLOG.critical(msg) raise
def main(): monkey_patch_black(Mode.synchronous) # Reach in and monkey patch the Click options. This is tricky based on the # way Click works! This is highly fragile because the index into the Click # parameters is dependent on the decorator order for Black's main(). # Change the default line length to 79 characters. line_length_param = black.main.params[1] assert line_length_param.name == 'line_length' line_length_param.default = 79 # Change the target version help doc to mention "Blue", not "Black". target_version_param = black.main.params[2] assert target_version_param.name == 'target_version' target_version_param.help = target_version_param.help.replace( 'Black', 'Blue') # Change the config param callback to support setup.cfg, tox.ini, etc. config_param = black.main.params[25] assert config_param.name == 'config' config_param.callback = read_configs # Change the version string by adding a redundant Click `version_option` # decorator on `black.main`. Fortunately the added `version_option` takes # precedence over the existing one. version_string = f'{__version__}, based on black {black.__version__}' version_option(version_string)(black.main) black.main()
def main(): """Runs White.""" sys.argv.extend(['--line-length', str(PEP8_LINE_LENGTH)]) black.main()
def main(*args, **kwargs) -> None: black.main.help = "Another uncompromising code formatter." black.main = click.version_option(version=__version__)(black.main) patch_format_str() black.patch_click() return black.main(*args, **kwargs)
print("############### pydocstyle #################") subprocess.call(["pydocstyle", MODULE_NAME, "--match=.*(?<!_pb2)\.py"]) print("") # pylint print("############### pylint #################") PYLINT_RESULTS = pylint.Run([f"./{MODULE_NAME}/"], do_exit=False) print("") # mypy print("############### mypy #################") MYPY_RESULTS = mypy.run( [f"./{MODULE_NAME}/", "--warn-redundant-casts", "--show-error-context"]) print(MYPY_RESULTS[0], end="") print(MYPY_RESULTS[1], end="") print("Exit code of mypy: {}".format(MYPY_RESULTS[2])) # # mypy # print("############ mypy tests ##############") # MYPY_RESULTS = mypy.run( # ["./tests/", "--warn-redundant-casts", "--show-error-context", "--check-untyped-defs"] # ) # print(MYPY_RESULTS[0], end="") # print(MYPY_RESULTS[1], end="") # print("Exit code of mypy: {}".format(MYPY_RESULTS[2])) # black print("############ black ##############") black.main(["-l", "100", "-t", "py36", "-S", "./", "--exclude=(.?env|.?venv)"]) print("")
# Copyright 2019 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """black""" import sys import black print("black: ", sys.argv) black.main(sys.argv[1:])
from typing import Dict import black from autocxxpy.core.cxxparser import CxxFileParser parser = CxxFileParser(files=[ "iTapAPIError.h", "TapAPIError.h", ], include_paths=['vntap/include']) result = parser.parse() error_map: Dict[int, str] = {} for v in result.g.variables.values(): error_map[v.value] = v.brief_comment for v in result.g.namespaces['ITapTrade'].variables.values(): error_map[v.value] = v.brief_comment result = f'error_map = {repr(error_map)}' output_file = 'error_codes.py' with open(output_file, "wt", encoding='utf-8') as f: f.write(result) black.main([output_file])
def main(): # behabe like normal black code sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(black.main())
def check(): import black black.main(["--check", root_dir])
def fmt(): import black black.main([root_dir])
def run(self): import black, shlex errno = black.main(shlex.split(self.black_args))
def macchiato(in_fp, out_fp, args=None): if args is None: args = [] # Read input. lines = in_fp.readlines() # Detect blank lines and deal with completely blank input. n_blank_before, n_blank_after = count_surrounding_blank_lines(lines) until = len(lines) - n_blank_after lines = lines[n_blank_before:until] if not lines: out_fp.write("\n" * n_blank_before) return 0 # Detect indentation. Add "if True:" lines if needed for valid syntax. first_line = lines[0] indent = len(first_line) - len(first_line.lstrip()) n_fake_before, remainder = divmod(indent, 4) if remainder: raise ValueError("indent of first line must be a multiple of four") for i in range(n_fake_before): prefix = 4 * i * " " lines.insert(i, f"{prefix}if True:\n") # Handle else/elif/except/finally try: first_token = next( tokenize.generate_tokens(iter([first_line.lstrip()]).__next__) ) except tokenize.TokenError: first_token = None if first_token and first_token.type == tokenize.NAME: name = first_token.string if name in {"else", "elif"}: lines.insert(n_fake_before, f"{indent * ' '}if True:\n") lines.insert(n_fake_before + 1, f"{indent * ' '} pass\n") n_fake_before += 2 elif name in {"except", "finally"}: lines.insert(n_fake_before, f"{indent * ' '}try:\n") lines.insert(n_fake_before + 1, f"{indent * ' '} pass\n") n_fake_before += 2 # Detect an unclosed block at the end. Add ‘pass’ at the end of the line if # needed for valid syntax. last_line = lines[-1] n_fake_after = 0 if last_line.rstrip().endswith(":"): lines[-1] = last_line.rstrip() + "pass\n" n_fake_after = 1 with tempfile.NamedTemporaryFile(suffix=".py", mode="wt+", delete=False) as fp: # Copy the input. for line in lines: fp.write(line) fp.flush() # Run black. if "--quiet" not in args: args.append("--quiet") args.append(fp.name) try: exit_code = black.main(args=args) except SystemExit as exc: exit_code = exc.code if exit_code == 0: # Write output. fp.seek(0) formatted_lines = fp.readlines() until = len(formatted_lines) - n_fake_after formatted_lines = formatted_lines[n_fake_before:until] fmt_n_blank_before, _ = count_surrounding_blank_lines(formatted_lines) formatted_lines = formatted_lines[fmt_n_blank_before:] out_fp.write("\n" * n_blank_before) for line in formatted_lines: out_fp.write(line) out_fp.write("\n" * n_blank_after) return exit_code
# mypy print("############### mypy #################") MYPY_RESULTS = mypy.run( ["./ethicml/", "--warn-redundant-casts", "--show-error-context"]) print(MYPY_RESULTS[0], end="") print(MYPY_RESULTS[1], end="") print("Exit code of mypy: {}".format(MYPY_RESULTS[2])) # mypy print("############ mypy tests ##############") MYPY_RESULTS = mypy.run([ "./tests/", "--warn-redundant-casts", "--show-error-context", "--show-error-codes", "--pretty" ]) print(MYPY_RESULTS[0], end="") print(MYPY_RESULTS[1], end="") print("Exit code of mypy: {}".format(MYPY_RESULTS[2])) # black print("############ black ##############") black.main([ "-l", "100", "-t", "py36", "-S", "./ethicml", "--config", ".black-config.toml" ]) black.main([ "-l", "100", "-t", "py36", "-S", "./tests", "--config", ".black-config.toml" ]) print("")