Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Arguments:
line_strings: (array of string) A list of strings representing a line
range like 'start-end'.
Returns:
A list of tuples of the start and end line numbers.
Raises:
ValueError: If the line string failed to parse or was an invalid line range.
"""
lines = []
for line_string in line_strings:
# The 'list' here is needed by Python 3.
line = list(map(int, line_string.split('-', 1)))
if line[0] < 1:
raise errors.YapfError('invalid start of line range: %r' % line)
if line[0] > line[1]:
raise errors.YapfError('end comes before start in line range: %r', line)
lines.append(tuple(line))
return lines
range like 'start-end'.
Returns:
A list of tuples of the start and end line numbers.
Raises:
ValueError: If the line string failed to parse or was an invalid line range.
"""
lines = []
for line_string in line_strings:
# The 'list' here is needed by Python 3.
line = list(map(int, line_string.split('-', 1)))
if line[0] < 1:
raise errors.YapfError('invalid start of line range: %r' % line)
if line[0] > line[1]:
raise errors.YapfError('end comes before start in line range: %r', line)
lines.append(tuple(line))
return lines
os.path.dirname(filename))
try:
reformatted_code, encoding, has_change = yapf_api.FormatFile(
filename,
in_place=in_place,
style_config=style_config,
lines=lines,
print_diff=print_diff,
verify=verify,
logger=logging.warning)
if not in_place and not quiet and reformatted_code:
file_resources.WriteReformattedCode(filename, reformatted_code, encoding,
in_place)
return has_change
except tokenize.TokenError as e:
raise errors.YapfError('%s:%s:%s' % (filename, e.args[1][0], e.args[0]))
except SyntaxError as e:
e.filename = filename
raise
def _GetExcludePatternsFromFile(filename):
"""Get a list of file patterns to ignore."""
ignore_patterns = []
# See if we have a .yapfignore file.
if os.path.isfile(filename) and os.access(filename, os.R_OK):
with open(filename, 'r') as fd:
for line in fd:
if line.strip() and not line.startswith('#'):
ignore_patterns.append(line.strip())
if any(e.startswith('./') for e in ignore_patterns):
raise errors.YapfError('path in .yapfignore should not start with ./')
return ignore_patterns
verify=args.verify)
except tokenize.TokenError as e:
raise errors.YapfError('%s:%s' % (e.args[1][0], e.args[0]))
file_resources.WriteReformattedCode('', reformatted_source)
return 0
# Get additional exclude patterns from ignorefile
exclude_patterns_from_ignore_file = file_resources.GetExcludePatternsForDir(
os.getcwd())
files = file_resources.GetCommandLineFiles(args.files, args.recursive,
(args.exclude or []) +
exclude_patterns_from_ignore_file)
if not files:
raise errors.YapfError('Input filenames did not match any python files')
changed = FormatFiles(
files,
lines,
style_config=args.style,
no_local_style=args.no_local_style,
in_place=args.in_place,
print_diff=args.diff,
verify=args.verify,
parallel=args.parallel,
quiet=args.quiet,
verbose=args.verbose)
return 1 if changed and (args.diff or args.quiet) else 0
def _FindPythonFiles(filenames, recursive, exclude):
"""Find all Python files."""
if exclude and any(e.startswith('./') for e in exclude):
raise errors.YapfError("path in '--exclude' should not start with ./")
python_files = []
for filename in filenames:
if filename != '.' and exclude and IsIgnored(filename, exclude):
continue
if os.path.isdir(filename):
if recursive:
# TODO(morbo): Look into a version of os.walk that can handle recursion.
excluded_dirs = []
for dirpath, _, filelist in os.walk(filename):
if dirpath != '.' and exclude and IsIgnored(dirpath, exclude):
excluded_dirs.append(dirpath)
continue
elif any(dirpath.startswith(e) for e in excluded_dirs):
continue
for f in filelist:
# TODO(morbo): Look into a version of os.walk that can handle recursion.
excluded_dirs = []
for dirpath, _, filelist in os.walk(filename):
if dirpath != '.' and exclude and IsIgnored(dirpath, exclude):
excluded_dirs.append(dirpath)
continue
elif any(dirpath.startswith(e) for e in excluded_dirs):
continue
for f in filelist:
filepath = os.path.join(dirpath, f)
if exclude and IsIgnored(filepath, exclude):
continue
if IsPythonFile(filepath):
python_files.append(filepath)
else:
raise errors.YapfError(
"directory specified without '--recursive' flag: %s" % filename)
elif os.path.isfile(filename):
python_files.append(filename)
return python_files