Skip to content

Commit

Permalink
Convert to snake case (7/n) (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI authored Feb 20, 2021
1 parent a36bb65 commit 0fbbae5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
58 changes: 30 additions & 28 deletions src/choose.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,66 @@
"""


def doProgram(
def do_program(
stdscr,
flags,
keyBindings: Optional[KeyBindings] = None,
cursesAPI=None,
lineObjs=None,
key_bindings: Optional[KeyBindings] = None,
curses_api=None,
line_objs=None,
) -> None:
# curses and lineObjs get dependency injected for
# our tests, so init these if they are not provided
if not keyBindings:
keyBindings = read_key_bindings()
if not cursesAPI:
cursesAPI = CursesAPI()
if not lineObjs:
lineObjs = getLineObjs()
if not key_bindings:
key_bindings = read_key_bindings()
if not curses_api:
curses_api = CursesAPI()
if not line_objs:
line_objs = get_line_objs()
output.clear_file()
logger.clear_file()
screen = screen_control.Controller(flags, keyBindings, stdscr, lineObjs, cursesAPI)
screen = screen_control.Controller(
flags, key_bindings, stdscr, line_objs, curses_api
)
screen.control()


def getLineObjs():
filePath = state_files.get_pickle_file_path()
def get_line_objs():
file_path = state_files.get_pickle_file_path()
try:
lineObjs = pickle.load(open(filePath, "rb"))
line_objs = pickle.load(open(file_path, "rb"))
except (OSError, KeyError, pickle.PickleError):
output.append_error(LOAD_SELECTION_WARNING)
output.append_exit()
sys.exit(1)
logger.add_event("total_num_files", len(lineObjs))
logger.add_event("total_num_files", len(line_objs))

selectionPath = state_files.get_selection_file_path()
if os.path.isfile(selectionPath):
setSelectionsFromPickle(selectionPath, lineObjs)
selection_path = state_files.get_selection_file_path()
if os.path.isfile(selection_path):
set_selections_from_pickle(selection_path, line_objs)

matches = [lineObj for lineObj in lineObjs.values() if not lineObj.is_simple()]
matches = [lineObj for lineObj in line_objs.values() if not lineObj.is_simple()]
if not matches:
output.write_to_file('echo "No lines matched!";')
output.append_exit()
sys.exit(0)
return lineObjs
return line_objs


def setSelectionsFromPickle(selectionPath, lineObjs):
def set_selections_from_pickle(selection_path, line_objs):
try:
selectedIndices = pickle.load(open(selectionPath, "rb"))
selected_indices = pickle.load(open(selection_path, "rb"))
except (OSError, KeyError, pickle.PickleError):
output.append_error(LOAD_SELECTION_WARNING)
output.append_exit()
sys.exit(1)
for index in selectedIndices:
if index >= len(lineObjs.items()):
for index in selected_indices:
if index >= len(line_objs.items()):
error = "Found index %d more than total matches" % index
output.append_error(error)
continue
toSelect = lineObjs[index]
if isinstance(toSelect, LineMatch):
lineObjs[index].set_select(True)
to_select = line_objs[index]
if isinstance(to_select, LineMatch):
line_objs[index].set_select(True)
else:
error = "Line %d was selected but is not LineMatch" % index
output.append_error(error)
Expand All @@ -98,7 +100,7 @@ def main(argv) -> int:
# so we can benefit from the default argparse
# behavior:
flags = ScreenFlags.init_from_args(argv[1:])
curses.wrapper(lambda x: doProgram(x, flags))
curses.wrapper(lambda x: do_program(x, flags))
return 0


Expand Down
54 changes: 27 additions & 27 deletions src/process_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,51 @@
from pathpicker.usage_strings import USAGE_STR


def getLineObjs(flags):
inputLines = sys.stdin.readlines()
return getLineObjsFromLines(
inputLines,
validateFileExists=not flags.get_disable_file_checks(),
allInput=flags.get_all_input(),
def get_line_objs(flags):
input_lines = sys.stdin.readlines()
return get_line_objs_from_lines(
input_lines,
validate_file_exists=not flags.get_disable_file_checks(),
all_input=flags.get_all_input(),
)


def getLineObjsFromLines(inputLines, validateFileExists=True, allInput=False):
lineObjs = {}
for index, line in enumerate(inputLines):
def get_line_objs_from_lines(input_lines, validate_file_exists=True, all_input=False):
line_objs = {}
for index, line in enumerate(input_lines):
line = line.replace("\t", " ")
# remove the new line as we place the cursor ourselves for each
# line. this avoids curses errors when we newline past the end of the
# screen
line = line.replace("\n", "")
formattedLine = FormattedText(line)
formatted_line = FormattedText(line)
result = parse.match_line(
str(formattedLine),
validate_file_exists=validateFileExists,
all_input=allInput,
str(formatted_line),
validate_file_exists=validate_file_exists,
all_input=all_input,
)

if not result:
line = SimpleLine(formattedLine, index)
line = SimpleLine(formatted_line, index)
else:
line = LineMatch(
formattedLine,
formatted_line,
result,
index,
validate_file_exists=validateFileExists,
all_input=allInput,
validate_file_exists=validate_file_exists,
all_input=all_input,
)

lineObjs[index] = line
line_objs[index] = line

return lineObjs
return line_objs


def doProgram(flags):
filePath = state_files.get_pickle_file_path()
lineObjs = getLineObjs(flags)
def do_program(flags):
file_path = state_files.get_pickle_file_path()
line_objs = get_line_objs(flags)
# pickle it so the next program can parse it
pickle.dump(lineObjs, open(filePath, "wb"))
pickle.dump(line_objs, open(file_path, "wb"))


def usage():
Expand All @@ -68,9 +68,9 @@ def main(argv) -> int:
flags = ScreenFlags.init_from_args(argv[1:])
if flags.get_is_clean_mode():
print("Cleaning out state files...")
for filePath in state_files.get_all_state_files():
if os.path.isfile(filePath):
os.remove(filePath)
for file_path in state_files.get_all_state_files():
if os.path.isfile(file_path):
os.remove(file_path)
print("Done! Removed %d files " % len(state_files.get_all_state_files()))
return 0
if sys.stdin.isatty():
Expand All @@ -84,7 +84,7 @@ def main(argv) -> int:
selection_path = state_files.get_selection_file_path()
if os.path.isfile(selection_path):
os.remove(selection_path)
doProgram(flags)
do_program(flags)
return 0


Expand Down
6 changes: 3 additions & 3 deletions src/tests/lib/screen_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def getLineObjsFromFile(inputFile, validateFileExists, allInput):
file = open(inputFile)
lines = file.read().split("\n")
file.close()
return process_input.getLineObjsFromLines(
lines, validateFileExists=validateFileExists, allInput=allInput
return process_input.get_line_objs_from_lines(
lines, validate_file_exists=validateFileExists, all_input=allInput
)


Expand Down Expand Up @@ -53,7 +53,7 @@ def getRowsFromScreenRun(
# we run our program and throw a StopIteration exception
# instead of sys.exit-ing
try:
choose.doProgram(
choose.do_program(
screen, flags, KEY_BINDINGS_FOR_TEST, CursesForTest(), lineObjs
)
except StopIteration:
Expand Down

0 comments on commit 0fbbae5

Please sign in to comment.