Skip to content

Commit

Permalink
- Add utils for the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jessielw committed Jul 23, 2023
1 parent 4104a2c commit be30ec5
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
Empty file added cli/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions cli/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# edit this file to control app name and version

program_name = "AutoQPF-CLI"
__version__ = "0.1.0-beta"
developed_by = "jlw4049"
23 changes: 23 additions & 0 deletions cli/exit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys

exit_success = 0
exit_fail = 1


def _exit_application(msg: str, exit_code: int = 0):
"""A clean way to exit the program without raising traceback errors
Args:
msg (str): Success or Error message you'd like to display in the console
exit_code (int): Can either be 0 (success) or 1 (fail)
"""
if exit_code not in {0, 1}:
raise ValueError("exit_code must only be '0' or '1' (int)")

if exit_code == 0:
output = sys.stdout
elif exit_code == 1:
output = sys.stderr

print(msg, file=output)
sys.exit(exit_code)
25 changes: 25 additions & 0 deletions cli/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pathlib import Path

from auto_qpf.qpf import QpfGenerator


def create_stax_rip_directory(file_input: Path):
# create staxrip temp directory if it doesn't exist
output_dir = Path(
Path(file_input).parent
/ Path(str(Path(file_input.name).with_suffix("")) + "_temp")
)
# print("printing output directory")
# print(output_dir)
output_dir.mkdir(exist_ok=True)

# define file out for staxrip output
file_output = output_dir / Path(file_input.name).with_suffix(".qpf")
return file_output


def process_args(file_input, file_output, fps, auto_detect_fps):
# print(file_input)
# print(file_output)
qpf = QpfGenerator().generate_qpf(file_input, file_output, fps, auto_detect_fps)
return qpf
87 changes: 87 additions & 0 deletions cli/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import argparse
import glob
from pathlib import Path


class CustomHelpFormatter(argparse.RawTextHelpFormatter):
"""Custom help formatter for argparse that modifies the format of action invocations.
Inherits from argparse.RawTextHelpFormatter and overrides the _format_action_invocation method.
This formatter adds a comma after each option string, and removes the default metavar from the
args string of optional arguments that have no explicit metavar specified.
Attributes:
max_help_position (int): The maximum starting column for the help string.
width (int): The width of the help string.
Methods:
_format_action_invocation(action): Overrides the method in RawTextHelpFormatter.
Modifies the format of action invocations by adding a comma after each option string
and removing the default metavar from the args string of optional arguments that have
no explicit metavar specified. Returns the modified string."""

def _format_action_invocation(self, action):
if not action.option_strings or action.nargs == 0:
return super()._format_action_invocation(action)

default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)

option_strings = ", ".join(action.option_strings)
return f"{option_strings}, {args_string}"


class FileParser:
def parse_input_s(self, args_list: list):
"""
Parse the input arguments and return a list of Path objects representing the input files.
Args:
args_list (list): List of input arguments.
Returns:
list: List of Path objects representing the input files.
Raises:
FileNotFoundError: If an input path is not a valid file path.
"""
input_s = []

# check for nested directories
args_list = self._get_files_from_directory(args_list)

# find all files
for arg_input in args_list:
arg_input = str(arg_input)
# non recursive
if "*" in arg_input:
input_s.extend(Path(p) for p in glob.glob(arg_input))

# recursive search
elif "**" in arg_input:
input_s.extend(Path(p) for p in glob.glob(arg_input, recursive=True))

# single file path
elif (
Path(arg_input).exists()
and Path(arg_input).is_file()
and arg_input.strip() != ""
):
input_s.append(Path(arg_input))
else:
raise FileNotFoundError(f"{arg_input} is not a valid input path.")

return input_s

@staticmethod
def _get_files_from_directory(file_s: list):
file_s_list = []
for input_file in file_s:
input_file = Path(input_file)
if input_file.is_dir():
for find_file in input_file.rglob("*"):
if find_file.is_file():
file_s_list.append(find_file)
elif input_file.is_file():
file_s_list.append(input_file)
return file_s_list

0 comments on commit be30ec5

Please sign in to comment.