Skip to content

Updated cli #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 52 additions & 77 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,59 @@
from parsers.sig import *
import sys


class bcolors:
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
WHITE = "\033[97m"
import sys, argparse


def main():
value = get_input()
generate_output(value)


def get_input():
while True:
try:
if len(sys.argv) == 1:
print_usage_instructions()
break
elif sys.argv[1] == "--b":
input_file, output_file = sys.argv[2], sys.argv[3]
return 2
else:
return 1
except IndexError:
print("Usage: main.py --b input.csv output.csv")
break


def print_usage_instructions():
instructions = [
(
bcolors.BOLD
+ bcolors.WHITE
+ "\n Individual sig usage: "
+ bcolors.ENDC
+ "main.py your sig goes here"
),
(
bcolors.BOLD
+ bcolors.WHITE
+ "\n Bulk sig usage: "
+ bcolors.ENDC
+ " main.py --b input.csv output.csv\n"
),
(
" Bulk sig instructions: \n > Place your input file in the /csv directory.\n"
" > Input files are read from the /csv directory.\n"
" > Output files are written to the /csv/output directory.\n"
" > Enter the input file name (input.csv as default) and output file name (output.csv as default), separated by a space.\n"
),
]
for instruction in instructions:
print(instruction)


def generate_output(n):
if n == 1:
print(SigParser().parse(" ".join(sys.argv[1:])))

elif n == 2:
try:
input_file, output_file = sys.argv[2], sys.argv[3]
if input_file.endswith(".csv") and output_file.endswith(".csv"):
SigParser().parse_sig_csv(input_file, output_file)
print(f"Output written to {output_file}.")
else:
print("Both files must end with .csv. Please try again.")
except ValueError:
print("Invalid. Enter input and output file names separated by a space.")
except FileNotFoundError:
print("Input file not found. Please try again.")
parser = setup_parser()
args = parser.parse_args()

if len(sys.argv) <= 1:
parser.print_usage()
sys.exit("Error: No arguments provided")
elif args.m:
generate_single()
elif args.b:
input_file, output_file = args.b
check_csv_files(args.b)
generate_bulk(input_file, output_file)
else:
sys.exit("An unknown error occured.")


def setup_parser():
parser = argparse.ArgumentParser(
prog="ParseRx",
description="A modern, lightweight medication sig parser.",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"-m", nargs=argparse.REMAINDER, help="""Enter a single sig to parse."""
)
parser.add_argument(
"-b",
nargs=2,
metavar=("input.csv", "output.csv"),
help="""Bulk sig instructions:
> Place your input file in the /csv directory.
> Input files are read from the /csv directory.
> Output files are written to the /csv/output directory.
> Enter the input file name (input.csv as default) and output file name (output.csv as default), separated by a space.""",
)
return parser


def check_csv_files(filenames):
for filename in filenames:
if not filename.endswith(".csv"):
raise argparse.ArgumentTypeError("All files must have a .csv extension")
return filenames


def generate_single():
print(SigParser().parse(" ".join(sys.argv[1:])))


def generate_bulk(input, output):
SigParser().parse_sig_csv(input, output)


if __name__ == "__main__":
Expand Down