本文整理汇总了Python中isort.SortImports方法的典型用法代码示例。如果您正苦于以下问题:Python isort.SortImports方法的具体用法?Python isort.SortImports怎么用?Python isort.SortImports使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类isort
的用法示例。
在下文中一共展示了isort.SortImports方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_imports
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def test_imports(self):
test_dir = os.path.dirname(__file__)
test_files = (
f for f in os.listdir(test_dir, absolute=True) if f.endswith('.py')
)
root_dir = os.path.dirname(test_dir)
root_files = (
f for f in os.listdir(root_dir, absolute=True)
if f.endswith('.py')
)
source_dir = os.path.join(root_dir, 'pottery')
source_files = (
f for f in os.listdir(source_dir, absolute=True)
if f.endswith('.py')
)
for f in itertools.chain(test_files, root_files, source_files):
if not f.endswith(self._EXCLUDES):
with self.subTest(f=f):
assert SortImports(f, check=True).correctly_sorted
示例2: format_string
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def format_string(cls, old_contents):
"""Format content of a file."""
new_contents = isort.SortImports(file_contents=old_contents).output
return old_contents, new_contents, 'utf-8'
示例3: format_code
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def format_code(self, code: str, notebook: bool, **options) -> str:
from isort import SortImports
return SortImports(file_contents=code, **options).output
示例4: sort_imports
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def sort_imports(file_name, **arguments):
try:
result = SortImports(file_name, **arguments)
return SortAttempt(result.incorrectly_sorted, result.skipped)
except IOError as e:
print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e))
return None
示例5: run
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def run(self):
arguments = self.arguments
wrong_sorted_files = False
arguments['check'] = True
for path in self.distribution_files():
for python_file in glob.iglob(os.path.join(path, '*.py')):
try:
incorrectly_sorted = SortImports(python_file, **arguments).incorrectly_sorted
if incorrectly_sorted:
wrong_sorted_files = True
except IOError as e:
print("WARNING: Unable to parse file {0} due to {1}".format(python_file, e))
if wrong_sorted_files:
exit(1)
示例6: __init__
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def __init__(self, linter: Optional[PyLinter] = None) -> None:
BaseChecker.__init__(self, linter)
self.isort_obj = isort.SortImports(
file_contents='',
)
示例7: run
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def run(self):
arguments = self.arguments
wrong_sorted_files = False
arguments['check'] = True
for path in self.distribution_files():
for python_file in glob.iglob(os.path.join(path, '*.py')):
try:
incorrectly_sorted = SortImports(python_file, **arguments).incorrectly_sorted
if incorrectly_sorted:
wrong_sorted_files = True
except IOError as e:
print("WARNING: Unable to parse file {0} due to {1}".format(python_file, e))
if wrong_sorted_files:
sys.exit(1)
示例8: git_hook
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def git_hook(strict=False, modify=False):
"""
Git pre-commit hook to check staged files for isort errors
:param bool strict - if True, return number of errors on exit,
causing the hook to fail. If False, return zero so it will
just act as a warning.
:param bool modify - if True, fix the sources if they are not
sorted properly. If False, only report result without
modifying anything.
:return number of errors if in strict mode, 0 otherwise.
"""
# Get list of files modified and staged
diff_cmd = "git diff-index --cached --name-only --diff-filter=ACMRTUXB HEAD"
files_modified = get_lines(diff_cmd)
errors = 0
for filename in files_modified:
if filename.endswith('.py'):
# Get the staged contents of the file
staged_cmd = "git show :%s" % filename
staged_contents = get_output(staged_cmd)
sort = SortImports(
file_path=filename,
file_contents=staged_contents.decode(),
check=True
)
if sort.incorrectly_sorted:
errors += 1
if modify:
SortImports(
file_path=filename,
file_contents=staged_contents.decode(),
check=False,
)
return errors if strict else 0
示例9: visitPrg
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def visitPrg(self, ctx):
if ctx.classDef():
self.class_list = [self.visit(classDef.classDefStart())[0] for classDef in ctx.classDef()]
if ctx.funcDef():
self.function_list = [self.visit(funcdef.funcDefStart())[0] for funcdef in ctx.funcDef()]
self.imports = ['from __future__ import division, print_function']
self.imports.append('from vfp2py import vfpfunc')
self.imports.append('from vfp2py.vfpfunc import DB, Array, C, F, M, S')
self.imports.append('from vfp2py.vfpfunc import parameters, lparameters, vfpclass')
defs = []
for i, child in enumerate(ctx.children):
if isinstance(child, ctx.parser.FuncDefContext):
funcname, decorator, funcbody = self.visit(child)
if i == 0 and funcname == '_program_main':
funcname = CodeStr('MAIN')
defs += [
add_args_to_code('@{}', (decorator,)),
add_args_to_code('def {}():', (funcname,)),
funcbody
]
if child.lineComment():
defs += sum((self.visit(comment) for comment in child.lineComment()), [])
elif not isinstance(child, antlr4.tree.Tree.TerminalNodeImpl):
defs += self.visit(child)
imports = isort.SortImports(file_contents='\n'.join(set(self.imports)), line_length=100000).output.splitlines()
return [CodeStr(imp) for imp in imports] + defs
示例10: apply_isort
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def apply_isort(code: str) -> str:
return SortImports(file_contents=code).output
示例11: _get_diff
# 需要导入模块: import isort [as 别名]
# 或者: from isort import SortImports [as 别名]
def _get_diff(self):
if self.treat_seperated_imports_independently:
import_stmts = PyImportSortBear._seperate_imports(self.file)
sorted_imps = []
for units in import_stmts:
sort_imports = SortImports(file_contents=''.
join([x[1] for x in units]),
**self.isort_settings)
sort_imports = sort_imports.output.splitlines(True)
sorted_imps.append((units, sort_imports))
diff = Diff(self.file)
for old, new in sorted_imps:
start = old[0][0]
end = start + len(old) - 1
diff.delete_lines(start, end)
assert isinstance(new, list)
diff.add_lines(start, list(new))
if diff.modified != diff._file:
return diff
else:
sort_imports = SortImports(file_contents=''.join(self.file),
**self.isort_settings)
new_file = tuple(sort_imports.output.splitlines(True))
if new_file != tuple(self.file):
diff = Diff.from_string_arrays(self.file, new_file)
return diff
return None