Page MenuHomec4science

appargs.py
No OneTemporary

File Metadata

Created
Tue, Mar 4, 14:38

appargs.py

import argparse
import os
import grp
import getpass
import sys
from sausage.functions import valid_date, valid_period
class AppArgs(object):
def __init__(self):
self.response = {}
self.parser = argparse.ArgumentParser(
prog="Sausage",
description="SCITAS Account Usage.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
self.add_args()
def add_args(self):
self.parser.add_argument(
"-u", "--user", help="If not provided whoami is considered"
)
self.parser.add_argument(
"-a",
"--all",
help="all users from an account are printed",
action="store_true",
)
self.parser.add_argument(
"-A", "--account", help="Prints account consumption per cluster"
)
self.parser.add_argument(
"-s", "--start", help="Start date - format YYYY-MM-DD", type=valid_date
)
self.parser.add_argument(
"-e", "--end", help="End date - format YYYY-MM-DD", type=valid_date
)
self.parser.add_argument(
"-c",
"--carbon",
help="Prints the carbon footprint per cluster",
action="store_true",
)
self.parser.add_argument(
"-b",
"--billing",
help="Displays the billing period - format YYYY-MM",
type=valid_period,
)
args = self.parser.parse_args()
# active = [k for k, v in vars(args).items() if v not in (None, False)]
if args.billing:
listofgroups = [grp.getgrgid(g).gr_name for g in os.getgroups()]
# if self.billinggrp not in listofgroups:
# self.parser.error(
# "--billing is only available for users in "
# + self.billinggrp
# + " group"
# )
if (
args.user
or args.account
or args.all
or args.start
or args.end
or args.carbon
):
self.parser.error("--billing is not compatible with any other option")
if args.start and args.end is None:
self.parser.error("range requires both dates (--start and --end)")
if args.end:
if args.start is None:
self.parser.error("range requires both dates (--start and --end)")
if args.end < args.start:
self.parser.error("start date must be earlier than end date")
if args.all and args.account is None:
self.parser.error(
"the option --all requires a valid account (--all and --account)"
)
if args.all and args.user:
self.parser.error("--all option is not compatible with --user option")
if len(sys.argv) <= 1:
args.user = getpass.getuser()
AppArgs.response = {
"user": args.user,
"account": args.account,
"all": args.all,
"start": args.start,
"end": args.end,
"carbon": args.carbon,
"billing": args.billing,
# "active": active,
}

Event Timeline