def _format_module_str(text: str, is_pyi=False) -> str: """Apply black and isort formatting to text.""" from black import FileMode, format_str from isort.api import sort_code_string text = sort_code_string(text, profile="black", float_to_top=True) text = format_str(text, mode=FileMode(line_length=79, is_pyi=is_pyi)) return text.replace("NoneType", "None")
def nblint_one(nb_node): """format/lint one notebook""" changes = 0 has_empty = 0 nb_metadata_keys = list(nb_node.metadata.keys()) for key in nb_metadata_keys: if key not in NB_METADATA_KEYS: nb_node.metadata.pop(key) for cell in nb_node.cells: cell_type = cell["cell_type"] source = "".join(cell["source"]) if not source.strip(): has_empty += 1 if cell_type == "markdown": args = [ *P.JLPM, "--silent", "prettier", "--stdin-filepath", "foo.md", "--prose-wrap", "always", ] prettier = subprocess.Popen( list(map(str, args)), stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) out, _err = prettier.communicate(source.encode("utf-8")) new = out.decode("utf-8").rstrip() if new != source: cell["source"] = new.splitlines(True) changes += 1 elif cell_type == "code": if cell["outputs"] or cell["execution_count"]: cell["outputs"] = [] cell["execution_count"] = None changes += 1 if [ line for line in source.splitlines() if line.strip().startswith("!") ]: continue if source.startswith("%"): continue new = sort_code_string(source, config=ISORT_CONFIG) new = blacken(new).rstrip() if new != source: cell["source"] = new.splitlines(True) changes += 1 if has_empty: changes += 1 nb_node.cells = [ cell for cell in nb_node.cells if "".join(cell["source"]).strip() ] return nb_node
def _sort_imports( self, content: str, ): """ Сортировка импортов при помощи isort """ return sort_code_string( code=content, config=getattr(settings, 'ISORT_CONFIG') or DEFAULT_CONFIG, )
def sort_imports( content: str, module_name: str, extension: str = "py", third_party: Iterable[str] = ()) -> str: """ Sort imports with `isort`. Arguments: content -- File content. module_name -- Current module name. extension -- py or pyi third_party -- List of module names to be marked as third-party. Returns: New file content. """ known_third_party = list(third_party) or [ "aiobotocore", "boto3", "botocore", "typing_extensions", "mypy_boto3", ] if module_name in known_third_party: known_third_party.remove(module_name) result = sort_code_string( code=content, extension=extension, config=Config( profile="black", known_first_party=[module_name], known_third_party=known_third_party, line_length=LINE_LENGTH, ), ) return result or ""
def test_sorted_imports_multiple_configs() -> None: with pytest.raises(ValueError): api.sort_code_string("import os", config=Config(line_length=80), line_length=80)
def main(argv=None, print_source=True): parser = argparse.ArgumentParser() parser.add_argument( "src_path", nargs="*", metavar="paths", type=str, help="Path/s to refactor. Glob supported enclosed in quotes", ) parser.add_argument("--start-from-last", action="store_true", help="Incremental refactor") parser.add_argument( "--limit-to", type=int, default=0, help="Stop processing after N files. Use with --start-from-last", ) parser.add_argument("--debug", action="store_true", help="Make verbose output") parser.add_argument("--show-only", action="store_true", help="write the result to stdin") parser.add_argument("--safe", action="store_true", help="Only move stdlib or thirdparty imports") parser.add_argument("--isort", action="store_true", help="Apply isort") args = parser.parse_args(argv) logging.root.setLevel(logging.DEBUG if args.debug else logging.INFO) all_files = chain.from_iterable( Path('.').glob(p) if not p.startswith("/") else [Path(p)] for p in args.src_path) last_processed = None if args.start_from_last: p = Path(".move-import") if p.exists(): last_processed = p.read_text().strip() logging.debug(f"found last_processed: {last_processed}") # discard until the last processed all_files = dropwhile(lambda x: str(x) != last_processed, all_files) next(all_files) # discard last_processed itself new_sources = [] for i, mod in enumerate(all_files): if args.limit_to and i == args.limit_to: break last_processed = mod logging.info(f"processing {mod}") new_source, new_head_end = refactor(mod, safe=args.safe) if args.isort: logging.debug("applying isort") # apply isort in black compatible mode new_source = sort_code_string( code=new_source, file_path=mod, combine_as_imports=True, multi_line_output=3, include_trailing_comma=True, force_grid_wrap=0, use_parentheses=True, line_length=120, ) if not args.show_only: mod.write_text(new_source) elif print_source: print(new_source) else: new_sources.append(new_source) if last_processed and args.start_from_last: p = Path(".move-import") p.write_text(str(mod)) return new_sources if not print_source else ""
def test_sort_code_string_mixed_newlines(): assert api.sort_code_string( "import A\n\r\nimportA\n\n") == "import A\r\n\r\nimportA\r\n\n"