python3izing scripts
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys,os,re
|
||||
import sys, os, re
|
||||
import difflib as dl
|
||||
|
||||
# Define color codes
|
||||
@@ -10,61 +10,65 @@ Reset = "\033[0m"
|
||||
# Available shortcut keys
|
||||
key_list = "1234567890qwertyuiop"
|
||||
|
||||
|
||||
class getch:
|
||||
def get(self):
|
||||
import termios, tty
|
||||
|
||||
fd = sys.stdin.fileno()
|
||||
old_settings = termios.tcgetattr(fd)
|
||||
try:
|
||||
tty.setraw(sys.stdin.fileno())
|
||||
ch = sys.stdin.read(1)
|
||||
finally:
|
||||
termios.tcsetattr(
|
||||
fd,
|
||||
termios.TCSADRAIN,
|
||||
old_settings
|
||||
)
|
||||
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
||||
return ch
|
||||
|
||||
|
||||
def printerr(s):
|
||||
sys.stderr.write(s + "\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
|
||||
def errexit(s):
|
||||
printerr(s)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def colorize_matches(directories, repl):
|
||||
return [repl.sub("%s\g<1>%s"%(Green, Reset), d) for d in directories]
|
||||
return [repl.sub("%s\g<1>%s" % (Green, Reset), d) for d in directories]
|
||||
|
||||
|
||||
def search_close_match(directories, key):
|
||||
""" Return a list of Components whose name closely matches key """
|
||||
match = []
|
||||
key = key.lower()
|
||||
for name in directories:
|
||||
s = dl.SequenceMatcher(
|
||||
None,
|
||||
name.lower(),
|
||||
key
|
||||
)
|
||||
s = dl.SequenceMatcher(None, name.lower(), key)
|
||||
if s.ratio() > 0:
|
||||
match.append(( s.ratio(), name ))
|
||||
match.sort(key = lambda x: x[0], reverse = True)
|
||||
best_matches=[x[1] for x in match[0:10]]
|
||||
match.append((s.ratio(), name))
|
||||
match.sort(key=lambda x: x[0], reverse=True)
|
||||
best_matches = [x[1] for x in match[0:10]]
|
||||
return best_matches
|
||||
|
||||
|
||||
def get_dirs(pat_dir):
|
||||
|
||||
# list sub-folders in the folder
|
||||
current_folders = [d for d in os.listdir(pat_dir) if os.path.isdir(os.path.join(pat_dir, d))]
|
||||
current_folders = [
|
||||
d for d in os.listdir(pat_dir) if os.path.isdir(os.path.join(pat_dir, d))
|
||||
]
|
||||
current_folders = [d for d in current_folders if not d.startswith(".")]
|
||||
current_folders.sort()
|
||||
return current_folders
|
||||
|
||||
|
||||
def main():
|
||||
# Print help
|
||||
if len(sys.argv) == 1 or sys.argv[-1] == "-h":
|
||||
errexit("Guess cd: Find first match in folder, interactive if multiple matches\n Arguments: [dir/]pattern_to_match_folders")
|
||||
errexit(
|
||||
"Guess cd: Find first match in folder, interactive if multiple matches\n Arguments: [dir/]pattern_to_match_folders"
|
||||
)
|
||||
|
||||
pattern = ".".join(sys.argv[1:])
|
||||
pat_base = os.path.basename(pattern)
|
||||
@@ -74,14 +78,14 @@ def main():
|
||||
pat_dir = "."
|
||||
|
||||
if not os.path.exists(pat_dir):
|
||||
errexit("%s: %sNo such folder%s"%(pat_dir, Red, Reset))
|
||||
errexit("%s: %sNo such folder%s" % (pat_dir, Red, Reset))
|
||||
|
||||
# list sub-folders in the folder
|
||||
current_folders = get_dirs(pat_dir)
|
||||
|
||||
# no sub-folders
|
||||
if len(current_folders) == 0:
|
||||
errexit("There are no subfolders in %s"%( pat_dir,))
|
||||
errexit("There are no subfolders in %s" % (pat_dir,))
|
||||
|
||||
# find matches
|
||||
match_case = re.compile(".*" + pat_base + ".*")
|
||||
@@ -92,8 +96,8 @@ def main():
|
||||
|
||||
# no matches, try case insensitive
|
||||
if len(matching_folders) == 0:
|
||||
match_nocase = re.compile(".*" + pat_base + ".*",re.I)
|
||||
repl_nocase = re.compile("(" + pat_base + ")",re.I)
|
||||
match_nocase = re.compile(".*" + pat_base + ".*", re.I)
|
||||
repl_nocase = re.compile("(" + pat_base + ")", re.I)
|
||||
matching_folders = [d for d in current_folders if match_nocase.match(d)]
|
||||
matches = colorize_matches(matching_folders, repl_nocase)
|
||||
|
||||
@@ -109,7 +113,7 @@ def main():
|
||||
sys.exit(0)
|
||||
|
||||
# Many matches, ask the user for folder index
|
||||
for i,m in enumerate(matches[0:len(key_list)]):
|
||||
for i, m in enumerate(matches[0 : len(key_list)]):
|
||||
printerr(key_list[i] + ": " + m)
|
||||
|
||||
if len(matches) > len(key_list):
|
||||
@@ -120,9 +124,10 @@ def main():
|
||||
key_index = key_list.index(key_in)
|
||||
key_match = matching_folders[key_index]
|
||||
except (ValueError, IndexError) as err:
|
||||
errexit("%s'%s' Not in the list%s"%(Red, key_in, Reset))
|
||||
errexit("%s'%s' Not in the list%s" % (Red, key_in, Reset))
|
||||
|
||||
print(os.path.join(pat_dir, key_match))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user