Skip to content

Commit

Permalink
fix is not matching pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
aktech committed Sep 12, 2023
1 parent d2a342e commit 2b1f9cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions bin/tidy-imports
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ def main():
options, args = parse_args(
_add_opts_and_defaults, import_format_params=True, modify_action_params=True, defaults=default_config)
def modify(x):
if options.canonicalize:
x = canonicalize_imports(x, params=options.params)
if options.transformations:
x = transform_imports(x, options.transformations,
params=options.params)
Expand All @@ -159,7 +157,10 @@ def main():
remove_unused=options.remove_unused,
add_mandatory=options.add_mandatory,
)
return sort_imports(x)
sx = sort_imports(x)
if options.canonicalize:
sx = canonicalize_imports(x, params=options.params)
return sx
process_actions(args, options.actions, modify)


Expand Down
14 changes: 7 additions & 7 deletions lib/python/pyflyby/_imports2s.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,19 +549,19 @@ def sort_imports(codeblock):
line_pkg_dict = {}
for i, line in enumerate(lines):
match = re.match(r'(from (\w+)|import (\w+))', line)
current_pkg = match.groups()[1:3]
current_pkg = current_pkg[0] if current_pkg[0] is not None else current_pkg[1]

pkg_lines[current_pkg].append(i)
line_pkg_dict[i] = current_pkg
if match:
current_pkg = match.groups()[1:3]
current_pkg = current_pkg[0] if current_pkg[0] is not None else current_pkg[1]
pkg_lines[current_pkg].append(i)
line_pkg_dict[i] = current_pkg

# Step 3: Create the output list of lines with blank lines around groups with more than one import
output_lines = []
for i, line in enumerate(lines):
if i > 0 and line_pkg_dict[i] != line_pkg_dict[i-1] and len(pkg_lines[line_pkg_dict[i]]) > 1:
if i > 0 and line_pkg_dict.get(i) != line_pkg_dict.get(i-1) and len(pkg_lines[line_pkg_dict.get(i)]) > 1:
output_lines.append('')
output_lines.append(line)
if i < len(lines) - 1 and line_pkg_dict[i] != line_pkg_dict[i+1] and len(pkg_lines[line_pkg_dict[i]]) > 1:
if i < len(lines) - 1 and line_pkg_dict.get(i) != line_pkg_dict.get(i+1) and len(pkg_lines[line_pkg_dict.get(i)]) > 1:
output_lines.append('')

# Step 4: Join the lines to create the output string
Expand Down

0 comments on commit 2b1f9cd

Please sign in to comment.