Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Xcode: Import fixes
#!/usr/bin/env python3
import os
match_keyword ="<some stuff here to find>"
import_line = "<some stuff needed to be imported>"
def find_first_line_match_idx(lines, match_str):
lastmatchIdx = None
idx = 0
for line in lines:
if match_str in line:
lastmatchIdx = idx
idx += 1
return lastmatchIdx
# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
print((len(path) - 1) * '---', os.path.basename(root))
for file in files:
path_to_file = root+"/"+file
with open(path_to_file, encoding = "ISO-8859-1") as f:
lines = f.readlines()
for line in lines:
if match_keyword in line:
print(len(path) * '---', file)
last_import_line_idx = find_first_line_match_idx(lines, "#import")
lines.insert(last_import_line_idx+1, import_line)
f = open(path_to_file, "w", encoding = "ISO-8859-1")
contents = "".join(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.